In iOS, variable property attributes indicate data accessibility and storage considerations,
1 2 3 4 5 6 7 8 9 10 |
|
- atomic (default)
- Only one thread access the variable (static type)
- Thread safe, but slow
Example :
1 2 |
|
- nonatomic
- Multiple threads access the variable (dynamic type)
- Thread unsafe, but fast
- Not a default behavior, need to add nonatomic keyword
Example:
1 2 |
|
- strong (iOS4 = retain, default)
- Own the object strongly, keep it in the heap until don’t point to it anymore
- Can’t dealloc this before aim fine with that same as “retain”
- Generally, using for UIViewControllers (UI item’s parents)
- Used with ARC and ARC automatically releases it when beyond its area or the strong reference is invalid
Example:
1 2 |
|
- weak (In iOS 4 & OS X Snow Leopand = unsafe_unretained )
- Keep it as long as someone else points to it strongly
- A “weak” reference is a reference that you don’t retain
- Can’t own the instance of object
- When the object is “deallocated”, the weak pointer is automatically set to nil
- Generally using for IBOutlets (UIViewController’s Childs) because the child object only needs to exist as long as the parent object does
Example :
1 2 |
|
- retain = strong
- Old value is released and it is assigned
- Specifies the new value should be sent “-retain” on assignment and the old value sent “-release”
- If you write retain it will auto work like strong
- Methods like “alloc” include an implicit “retain”
Example:
1 2 |
|
- assign (default)
- A property attribute tells the compiler how to synthesize the property’s setter implementation
Example:
1 2 |
|
- unsafe_unretained (In iOS 4 & OS X Snow Leopand)
- An ownership qualifier that tells ARC how to insert retain/release calls
- The ARC version of assign
- The old version of weak but not safe
Example:
1 2 |
|
- copy
- Required when the object is mutable
- Specifies the new value should be sent “-copy” on assignment and the old value should be sent “-release”
- Like retain, returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments
- Need to release the object when finished with it because you are retaining the copy
Example:
1 2 |
|
- readonly
- Tell compiler not to generate “setter” method automatically
- If you specify readonly, only a “getter” method is required in the @implementation block
- If you use the @synthesize directive in the @implementation block, only the “getter” method is synthesized
Example:
1 2 |
|
- readwrite (default)
- “setter” and “getter” are both generated
- Both “setter” and “getter” method are required in the @implementation block
- If you use the @synthesize directive in the implementation block, both the “setter” and “getter” methods are synthesized
Example:
1 2 |
|