私有接口与私有方法 - Objective C
私有方法和私有接口有什么区别? 例如,我知道如果你在实现中定义了一个方法,而它的接口没有提到它,那么它就被认为是私有方法。我还看到了诸如以下内容:
@interface Collector()
@property (readonly) NSMutableDictionary *count;
@end
.m 实现文件内部。
What is the difference between a private method and a private interface?
For example, I know that if you define a method in an implementation and its interface does not mention it, it is considered a private method. I have also seen things such as:
@interface Collector()
@property (readonly) NSMutableDictionary *count;
@end
Inside of the .m implementation file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@interface Foo()
在接口 Foo 上创建一个类扩展(我的观点是正确的,支持 bbum),就像添加到接口中的附加方法一样。有些人还使用@interafce Foo(Private)
(类别)而不是带有()
的类扩展。它更像是从类外部“注入”新方法到类中。将其放在 .m 文件中只会阻止其他内容在 .h 文件中“看到它”,但仅此而已。基本上,人们通常在 .m 文件中使用类别或类扩展来指定私有接口,但它们也用于 UIKit 使用类别来添加
row
和section
公共方法到NSIndexPath。 (这可能会令人困惑。)您实际上并不需要以这种方式定义私有方法,但是如果您有一个名为 bar 的方法,该方法在源文件中定义 foo 之前调用方法 foo,您将收到类似“对象 self 可能不会响应 foo”。您可以通过在定义 bar 或任何其他 foo 调用代码之前定义 foo 来摆脱这个问题。这对于普通的 C 和函数来说是一样的。
就像 Ole 所说,这并不能阻止任何人调用私有方法,它只是声明您的意图是它们是私有的,并导致编译器生成“可能不会响应”警告,即使它们导入 .h 文件也是如此。
编辑
另请参阅http://www.friday.com/bbum /2009/09/11/class-extensions-explained/ 有关类别与类扩展的一些解释。从编译器警告的角度来看,类扩展对于定义私有方法应该更正确,因为类别方法是可选的。希望我的书能解释这一点!
@interface Foo()
creates a class extension (I stand corrected, props to bbum) on interface Foo which is like additional methods added to the interface. Some people also use@interafce Foo(Private)
(category) instead of a class extension with()
. It's more like "injecting" new methods into a class from outside the class.Placing this in the .m file just keeps other things from "seeing it" in the .h file, but that's it. Basically people normally use categories or class extensions in .m files to specify private interfaces, but they are also used for things like UIKit uses categories to add
row
andsection
public methods to NSIndexPath. (This can be confusing.)You don't really need to define private methods this way, but if you have a method called bar that calls method foo before foo is defined in the source file you'll get a compiler warning something like "object self may not respond to foo". You can get rid of that by defining foo before you define bar or any other foo-calling code. It's the same with plain C and functions.
Like Ole says this doesn't stop anyone from calling the private methods, it just declares your intention that they be private and causes the compiler to generate the "may not respond to" warnings even if they import the .h file.
EDIT
Also see http://www.friday.com/bbum/2009/09/11/class-extensions-explained/ for some explanation of categories vs. class extensions. Looks like class extensions should be more correct for defining private methods, from a compiler warning perspective, because category methods are optional. Wish my book would have explained this!
Objective-C 没有完全私有的方法。 .m 文件中私有接口部分中声明的方法对于外部调用者不可见,但它不是私有的。如果有人知道方法签名并忽略编译器警告,他们可以从外部调用它而不会出现问题。
Objective-C has no totally private methods. The method declared in a private interface section in the .m file is invisible to outside callers but it is not private. If someone knows the method signature and ignores the compiler warning, they can call it from outside without problems.