因此,属性的自动综合非常棒。但是,当您同时提供一个getter和setter时,会出现错误。
@property (strong, nonatomic) NSArray *testArray;
- (NSArray *)testArray {
return _testArray;
}
- (void)setTestArray:(NSArray *)testArray {
_testArray = testArray;
}
错误:
Use of undeclared identifier '_testArray'
。添加
@synthesize testArray = _testArray;
可解决此问题。我只是想知道为什么会这样? 最佳答案
当您同时提供getter和setter时,通常根本就不需要实例变量,即,仅转发这些消息或将数据存储在其他位置时。
一旦缺少其中之一,就需要使用ivar来合成该功能。
如果我没记错的话,对于只读属性,模拟假设也成立。
关于objective-c - 提供getter和setter时,为什么需要编写@synthesize?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12918388/