Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use properties in Objective-C?

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?

like image 961
kiran kumar Avatar asked Mar 02 '11 18:03

kiran kumar


1 Answers

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.

like image 72
David Schaefgen Avatar answered Sep 21 '22 23:09

David Schaefgen