Can someone explain every word of this Objective-C property declaration?
I'm not开发者_如何学Python familiar with Objective-C syntax, so could someone explain what every term means in the following line of code?
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
- @property - is a objective-c syntax for declaring and optionally synthesizing accessor methods for instance variable. Read official reference for more details.
- nonatomic - means that synthesized getter method will return ivar directly without locking it for thread-safety
- retain - means that ivar will be retained in setter method
- readonly - the trick to make setter method "private" for class users - so only getter method will be visible for compiler. Usually in implementation file this property is redeclared without readonly attribute using custom category so that setter method can be used inside class itself.
- NSManagedObjectContext* - variable type
- managedObjectContext - variable name
Once again - best advice here is to read Properties section in official documentation...
readonly accessor means only the getter method is synthesised. The value is read only.
retain accessor means this class retains the pointer, the previous value is released
nonatomic accessor means that no locking is applied to the synthesised getter
@property reveals this member for synthesis so that getters/setters can be created. Without it you would have to access the property directly.
NSManagedObjectContext is a type of object/class, and *managedObjectContext is a pointer to an instance of that object.
Vladimir is correct.
But I will suggest you to read Objective-C 2.0 reference First for long run.
精彩评论