hzx*_*zxu 11 implementation private objective-c class-extensions
我有以下类接口:
@interface MyClass : NSObject
@property int publicProperty;
@end
Run Code Online (Sandbox Code Playgroud)
那么实施:
@interface MyClass() // class extension
- (void)privateMethod; // private methods
@end
@implementation MyClass {
int _privateProperty;
}
@property int privateProperty = _privateProperty;
@end
Run Code Online (Sandbox Code Playgroud)
这就是Apple在WWDC中展示的内容,但是有没有理由不将_privateProperty放在类扩展中,如:
@interface MyClass() // class extension
{
int _privateProperty;
}
- (void)privateMethod; // private methods
@end
Run Code Online (Sandbox Code Playgroud)
谢谢!
Jam*_*ter 10
我通常在实施中"强制"私有扩展
在你的标题中
@interface MyClass : NSObject
{
}
@property (nonatomic, assign) int publicProperty;
@end
Run Code Online (Sandbox Code Playgroud)
在您的实现文件中:
@interface MyClass ()
@property (nonatomic, assign) int privateProperty;
@end
@implementation MyClass
@synthesize privateProperty;
@synthesize publicProperty;
@end
Run Code Online (Sandbox Code Playgroud)
你不必在接口和实现中声明你的ivars.因为你想把它们设为私有你可以在实现文件中声明它们,如下所示:
@implementation {
int firstVariable;
int secondVariable;
...
}
//properties and code for your methods
Run Code Online (Sandbox Code Playgroud)
如果您愿意,可以创建getter和setter方法,以便可以访问这些变量.
你说话的人是对的,以为你没有任何理由不在界面中以相同的方式声明它们.有些书实际上告诉你@interface显示了该类的公共面,你在实现中所拥有的将是私有的.
| 归档时间: |
|
| 查看次数: |
7412 次 |
| 最近记录: |