加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Objective-C中的私有方法,在Xcode 4.3中我不再需要在我的实现文

发布时间:2020-12-15 01:53:37 所属栏目:百科 来源:网络整理
导读:我头上有很多问号。 我没有得到的是在xcode 4.3之前,我需要在我的实现文件中声明前向声明(对于私有方法)。 像我的.m文件: // deleting this with xcode 4.3 the below code still does work // in previous versions i had to put this because otherwise
我头上有很多问号。
我没有得到的是在xcode 4.3之前,我需要在我的实现文件中声明前向声明(对于私有方法)。

像我的.m文件:

// deleting this with xcode 4.3 the below code still does work 
// in previous versions i had to put this because otherwise the compiler can't find methodFirst
@interface DetailViewController ()
- (void)methodFirst;
- (void)methodSecond;
@end


@implementation DetailViewController

- (void) methodSecond
{
   // if i delete the forward declaration now adays i dont get a compiler error that he cant find method first
   [self methodFirst];
}

- (void) methodFirst
{
}

@end

现在看来我不再需要这样做了吗?苹果是否更新编译器,以便不再需要提出声明?

我没有找到任何关于这个变化的官方Apple源代码。我不知道其他人在新的环境中遇到了什么。

解决方法

从Xcode 4.3附带的LLVM编译器版本开始,如果您尝试调用编译器以前未曾看到的方法,它将查看当前的@implementation块的其余部分,以查看该方法是否在以后声明。如果是这样,那么它使用它,你不会得到警告。因此,从Xcode 4.3开始,不需要预先声明内部方法。显然,您仍然需要声明公开暴露于其他类的方法。

一些Xcode 4.3 beta的发行说明中注意到这一变化,但显然没有将其纳入“Xcode 4.3的新功能”最终文档。

与其他答案中建议的不同,这不仅仅是默认情况下已关闭的“未声明的选择器”警告。实际上,如果您使用ARC,则无法识别的选择器仍然是硬错误。尝试调用[self myNonexistentMethod],你会看到;编译器仍然抱怨。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读