When should I use the nonatomic
, retain
, readonly
and readwrite
properties in Objective-C?
For example:
@property(nonatomic, retain) NSObject *myObject;
If I use nonatomic
and retain
, does this mean that the object will be retained?
First off, I wanted to promote the comment from David Gelhar to a full answer. The modifiers atomic
and nonatomic
have nothing to do with thread safety. See this question for more detail in that space.
The other items you listed can be addressed relatively simply. I'll hit them briefly and point you toward the documentation on property modifiers if you want more.
atomic
vs nonatomic
primarily ensures that complete values are returned from synthesized getters and that complete values are written by synthesized setters.
readwrite
vs readonly
determines whether a synthesized property has a synthesized accessor or not (readwrite
has a setter and is the default, readonly
does not).
assign
vs retain
vs copy
determines how the synthesized accessors interact with the Objective-C memory management scheme. assign
is the default and simply performs a variable assignment. retain
specifies the new value should be sent -retain
on assignment and the old value sent -release
. copy
specifies the new value should be sent -copy
on assignment and the old value sent -release
.
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