Objective-C 中的私有方法,在 Xcode 4.3 中我不再需要在我的实现文件中声明它们?
我的脑海里浮现出很多问号。 我没有得到的是,在 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 是否更新了编译器,以便不再需要提出声明?
我找不到任何有关此更改的 Apple 官方消息来源。我想知道其他人在新环境中遇到了什么。
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.
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
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Xcode 4.3 附带的 LLVM 编译器版本开始,如果您尝试调用编译器以前未见过的方法,它将在当前
@implementation
块的其余部分中查找该方法是否已于稍后宣布。如果是这样,那么它就会使用它,并且您不会收到警告。因此,从 Xcode 4.3 开始,预先声明内部方法的需要大大减少。显然,您仍然需要声明公开暴露给其他类的方法。一些 Xcode 4.3 beta 的发行说明中注意到了这一变化,但显然没有将其纳入“Xcode 4.3 中的新增功能”最终文档中。
与其他答案中建议的不同,这不仅仅是默认情况下关闭的“未声明的选择器”警告。事实上,如果您使用 ARC,无法识别的选择器仍然是硬错误。尝试调用
[self myNonexistentMethod]
你会发现;编译器仍然抱怨。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.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.
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.Objective-C 中没有任何私有方法。正在考虑的是类延续,您可以在
.m
文件中声明“无名”类别接口,以声明将在类实现中但不在公共接口中的方法。在使用方法之前从来没有要求声明方法。然而,这始终是一个好主意,编译器有一个警告标志来指示何时使用未见过的方法。原因与操作系统的函数调用约定有关。不同的类型,例如结构体、浮点数、整数和指针,当它们作为函数的参数或函数的返回值时,都可以以不同的方式进行处理。事实上,在不同的计算机和不同的操作系统中,它们的处理方式不同。要知道如何处理 Objective-C 方法的参数和返回值,编译器需要知道该方法的签名:它采用多少个类型的参数,以及它返回什么类型。
如果它没有看到该方法的声明,那么编译器将需要进行猜测。如果这种猜测不正确,那么它最终可能会将错误的值放入参数中,或者错误地解释返回值,或者尝试从堆栈中取出不存在的内容。
There aren't any private methods in Objective-C. What you're thinking of is the class continuation, the "nameless" category interface you can declare in your
.m
file to declare methods that will be in the class implementation, but that aren't in the public interface.There's never been a requirement to declare methods before they're used. However, it's always been a good idea, and the compiler has a warning flag to indicate when methods that haven't been seen are used. The reason is to do with the operating system's calling convention for functions. Different types, such as structures, floating point numbers, integer numbers, and pointers, can all be handled in different ways when they are the arguments to or return values from functions. Indeed, on different computers and in different operating systems, they are handled in different ways. To know how to handle the arguments and return values for an Objective-C method, the compiler needs to know the signature for that method: how many arguments of what types it takes, and what type it returns.
If it hasn't seen a declaration of the method, then the compiler will need to make a guess. If that guess is incorrect, then it can end up putting the wrong values into the arguments, or interpreting the return value incorrectly, or trying to take something off the stack that doesn't exist.