【发布时间】:2011-10-01 13:32:24
【问题描述】:
在 Objective-C 中,有一种在 .m 文件中创建私有方法的骇人听闻的方法:
@interface FooClass (PrivateMethods)
- (void) doBar:(id)barAction withBaz:(id)bazAction;
@end
这对方法很有用。我试图对一个属性做同样的事情:
@interface BarClass (PrivateMethods)
@property (nonatomic, strong) BazObject *myBaz;
@end
@implementation BarClass
@synthesize myBaz = _myBaz;
[...]
@end
这带来了一个编译警告:在“PrivateMethods”类别中声明的属性不能在类实现中实现。我试图将我的财产转移到一个类别中:
@implementation BarClass (PrivateMethods)
@synthesize myBaz = _myBaz;
@end
然后:@synthesize is not allowed in a category's implementation.
显而易见的答案是“放弃尝试,只使用 ivars”,但 Apple 的员工告诉我,他们(个人)已经转向完全使用属性。他们带来的安全感(就像枪上的安全感,更难射中自己的脚)让我内心都很开心,那么有什么方法可以做到这一点而不诉诸裸体 ivars?
【问题讨论】:
标签: objective-c properties private