在Xcode 4.3中的Objective-C中的私有方法中,我不再需要在实现文件中声明它们? [英] Private Methods in Objective-C, in Xcode 4.3 I no longer need to declare them in my implementation file ?

查看:94
本文介绍了在Xcode 4.3中的Objective-C中的私有方法中,我不再需要在实现文件中声明它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的头顶上有很多问号. 我没有得到的是在xcode 4.3之前,我需要在实现文件中声明前向声明(对于私有方法).

I have a lot question marks tolling above my head. What I don't get is before xcode 4.3 I needed to declare forward declarations (for private methods) in my implementation file.

就像我的.m文件一样:

Like in my .m file:

// 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是否更新了编译器,以便不再需要提出声明了?

Now it seems I don't need to do that anymore? Did Apple update the compiler so that it isn't needed anymore to put forward declarations?

我找不到有关此更改的任何Apple官方消息来源.我想知道其他人在新环境中遇到了什么.

I can't find any reference to an official Apple source about this change. I wonder what other people have encountered in their new environment.

推荐答案

从Xcode 4.3附带的LLVM编译器版本开始,如果您尝试调用以前未见过的方法,它将在其余部分中查找.当前的@implementation块,以查看该方法是否在以后声明.如果是这样,那么它将使用它,并且您不会收到警告.因此,从Xcode 4.3开始,几乎不需要预先声明内部方法.显然,您仍然需要声明公开给其他类的方法.

As of the LLVM Compiler version shipped with Xcode 4.3, if you try to call a method that the compiler has not previously seen, it will look in the rest of the current @implementation block to see if that method has been declared later. If so, then it uses that, and you don't get a warning. Hence, as of Xcode 4.3, there's much less need to pre-declare your internal methods. Clearly, you still need to declare methods that are publicly exposed to other classes.

此更改已在某些Xcode 4.3 beta版本的发行说明中指出,但显然并未纳入"Xcode 4.3的新功能"最终文档中.

This change was noted in the release notes of some of the Xcode 4.3 betas, but apparently didn't make it into the "What's New in Xcode 4.3" final documentation.

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

Unlike has been suggested in other answers, this is not just an "Undeclared Selector" warning that has been turned off by default. In fact, if you're using ARC, unrecognized selectors are still hard errors. Try calling [self myNonexistentMethod] and you'll see; the compiler still complains.

这篇关于在Xcode 4.3中的Objective-C中的私有方法中,我不再需要在实现文件中声明它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆