So the auto synthesize of properties is awesome. However, when you provide both a getter and a setter, you get an error.
@property (strong, nonatomic) NSArray *testArray;
- (NSArray *)testArray {
return _testArray;
}
- (void)setTestArray:(NSArray *)testArray {
_testArray = testArray;
}
Error: Use of undeclared identifier '_testArray'
.
Adding @synthesize testArray = _testArray;
solves the problem. I am just wondering why this is?
@synthesize tells the compiler to take care of the accessor methods creation i.e it will generate the methods based on property description. It will also generate an instance variable to be used which you can specify as above, as a convention it starts with _(underscore)+propertyName.
It will generate an instance variable for the property's storage, using the naming convention with the leading underscore, or use an instance variable of that name if you explicitly declared one. It will also generate the accessor methods with the proper semantics to match the @property declaration.
Swift provides no differentiation between properties and instance variables (i.e, the underlying store for a property). To define a property, you simply declare a variable in the context of a class.
@dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime).
When you provide both getter and setter, there is often just no need for an instance variable at all, i.e. when you just forward those messages or store data in other places.
As soon as one of them is missing, the ivar is needed to synthesize that functionality.
If I remember correctly, for readonly properties the analogue assumption holds as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With