iOS property declaration clarification
Here are the only property modifiers that Xcode recognizes:
nonatomic(does not enforce thread safety on the property, mainly for use when only one thread shall be used throughout a program)
atomic(enforces thread safety on the property, mainly for use when multiple threads shall be used throughout a program) (default)
strong(automatically retains / releases values on set, makes sure values do not deallocate unexpectedly) (default if ARC and object type)
readonly(cannot set property)
readwrite(can both set and get property) (default)
unsafe_unretained(no memory management shall be done with this property, it is handled manually by the person assigning the value) (default if not ARC or object type)
copy(copies the object before setting it, in cases where the value set must not change due to external factors (strings, arrays, etc).
weak(automatically zeroes the reference should the object be deallocated, and does not retain the value passed in)
getter=method(sets the selector used for getting the value of this property)
setter= method(set the selector used for setting the value of this property)
1) @property is a special way to define getter- and setter-methods, or as we call them accessors in Objective-C. Your first snippet just declares an array for which you have to declare and write accessors yourself. For example
Using @property will declare your accessors for you and is equivalent to declaring
myArray yourself. It is the preferred way to declare accessors since Objective-C 2.0. Note that you still have to declare the property (in your case myArray) yourself.
2) You first need to know about @synthesize. Remember that @property DECLARES the accessors for your property, @synthesize will IMPLEMENT them. When you use an @property in your @interface you mostly likely write an @synthesize in @implementation. Using @synthesize is equivalent to implementing
(nonatomic, retain) tell the compiler, among others, how the memory management should work and therefore how the methods will be implemented. Note that you never actually see these accessors, but be assured that they are there and ready for you to be used.
Also you should familiarize yourself with at least the following attributes:
readonlyis set, ONLY the getter methods will be available.
- Setter Memory Management
assign(default), simply assigns the new value. You mostly likely only use this with primitive data types.
retain, releases the old value and retains the new. If you use the garbage collector,
retainis equivalent to
assign. Why? The manual release of the old value will be done by the garbage collector.
copywill copy the new value and release the old value. This is often used with strings.
atomic(default) will ensure that the setter method is atomic. This means only one thread can access the setter at once.
nonatomic, use this when you don't work with threads.
This post gives you a good introduction to memory management and
Properties are basically the accessor methods. They define the scope of the variable.First case as given above,the variable is not accessible in other classes whereas by declaring a property as in the second case,variable is accessible in other classes also.Also, they are useful for memory management.