我听说最近可以通过在.m文件中再次声明接口来创建 private 方法。但是语法到底是什么样的?
如果很重要:在ARC下。
最佳答案
头文件:
//YourClass.h:
@interface YourClass {
@private //optional
//private scope ivars
@protected //default, optional
//protected scope ivars
@public //optional
//public scope ivars
@package //optional
//package scope ivars
}
//public methods
@end
实施文件:
//YourClass.m:
#import "YourClass.h"
//you could also import this class extension (that's what it's called) from an
//external header file which can be helpful for making pseudo-protected methods/ivars
//Don't forget the additional import statement then, though.
@interface YourClass () <PrivateProtocol> //protocol tag optional, of cource
@private //optional
//private scope hidden ivars
@protected //default, optional
//protected scope hidden ivars
@public //optional
//public scope hidden ivars
@package //optional
//package scope hidden ivars
@end
@implementation YourClass
//your class' method implementations
@end
在ivar范围上使用Docs。
有关上述摘要中的新增功能及其全部内容(兼容性等)的更多信息,请参见 WWDC 2011 Session #322
Xcode 4.2带来了隐藏的 private ivars。
类扩展已经存在了一段时间。