I understand what read and write do when used as a properties but I’m confused with the rest.
- What is nonatomic and what is it used for?
- What is retain and what is it used for?
- Why is copy used? Wouldn’t it be equivalent to “read” since read also returns the value of the variables?
- What is assign used for? Wouldn’t it be equivalent to “write” since both set copies of the variable?
- Properties basically create getter and setter functions. Do they however create actual functions or do I just access the variable as in
[class variable]? (instead of[class getVariable]or[class setVariable:int variable]. If I am just directly accessing the variable isn’t this the equivalent of making the variable public?
You need to know which flags are alternatives to each other:
By default, getters and setters are thread-safe which also incurs a performance penalty. nonatomic tells the compiler to not worry about thread-safety considerations when writing getters and setters for that property. It will be faster, and should be preferred if you will only access that property from a given thread, usually the main thread.
assign tells the compiler to generate a setter that does not retain the new value. You should use it for primitive properties (
int,BOOL, etc). copy creates a new copy of the object when it is assigned. retain calls[newValue retain]on the new value of the object and[oldValue release]on the old value of the object in the setter.For completeness, readonly tells the compiler to only generate a getter, not a setter, whereas readwrite generates both.
When you access the variable using
[class variable], you are calling a function with the same name as the property. This lets you implement the getter or setter at a future point with your own implementation, without having to modify your code in numerous other places.