Properties in Objective-C are used to store data in instances of classes. They define memory management, type, and access attributes of the values they store such as strong , weak , assign , readonly , readwrite , etc.
atomic
// Defaultnonatomic
strong = retain
// Defaultweak = unsafe_unretained
retain
assign
// Defaultunsafe_unretained
copy
readonly
readwrite
// Default
In the article Variable property attributes or modifiers in iOS you can find all the above-mentioned attributes, and that will definitely help you.
atomic
atomic
means only one thread access the variable (static type).atomic
is thread safe.- But it is slow in performance
atomic
is the default behavior- Atomic accessors in a non garbage collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value.
- It is not actually a keyword.
Example:@property (retain) NSString *name; @synthesize name;
nonatomic
nonatomic
means multiple thread access the variable (dynamic type).nonatomic
is thread-unsafe.- But it is fast in performance
nonatomic
is NOT default behavior. We need to add thenonatomic
keyword in the property attribute.- It may result in unexpected behavior, when two different process (threads) access the same variable at the same time.
Example:@property (nonatomic, retain) NSString *name; @synthesize name;
- 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.- 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.
0 Comments