In property declarations in Objective-C, we need to be specific about the attributes of that property.
@property(nonatomic, retain) UITextField *userName; @property(atomic, retain) UITextField *userName; @property(retain) UITextField *userName;
nonatomic vs atomic
“atomic” is the default if it is not specified. We always use “nonatomic”. You can use this attribute to specify that accessor methods are not atomic. (There is no keyword to denote atomic.) Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently.
retain vs. copy vs. assign
- “assign” is the default. In the setter that is created by @synthesize, the value will simply be assigned to the attribute. My understanding is that “assign” should be used for non-pointer attributes.
- “retain” is needed when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count) the object. You will need to release the object when you are finished with it.
- “copy” is needed when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don’t want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.
Is that all? Nope… apparently, we can also specify our property’s access rights.
readwrite vs readonly
“readwrite” is the default. When you @synthesize, both a getter and a setter will be created for you. If you use “readonly”, no setter will be created. Use it for a value you don’t want to ever change after the instantiation of the object. So if for any reason, you need a property to be readonly, it will look something like this:-
@property(nonatomic, retain) UITextField *userName;