Though the question is basic, but I found it very crucial to understand to proceed with IOS programming. Sometimes we used to declare only instance variable and we don't set any associated property to it. Some where we just declare the properties and use synthesize to get or set the values. Sometimes I feel necessary to declare both in code, when compilation gives me warnings! What's the fundamental behind the manipulation of properties in Objective C. I know the basic requirement to create getter and setter for any instance variable, but when? I have seen it many times we don't use property at all, and after that too we easily set and get variable's value. Also, different types of property like atomic, non atomic, strong, retain are very unclear to me. XCODE up-gradation to 4.2 has shaken my concepts about memory management. Can somebody please clear the cloud over my mind?
Asked
Active
Viewed 1,907 times
4
-
http://www.raywenderlich.com/tutorials Check this site for awesome tutorials..I learned many concepts from this site. – Aaron Feb 20 '12 at 08:54
2 Answers
9
Properties are always the preferred way over direct ivar access, mostly of following reasons:
- You can override a getter or setter in a subclass
- You can define the "assigning behavior" (namely
copy
,assign
,retain/strong
,weak
) - You can synchronize ivar access
Keywords:
copy
: The object is copied to the ivar when setassign
: The object's pointer is assigned to the ivar when setretain/strong
: The object is retained on setweak
: In ARC this is like assign, but will be set tonil
automatically when the instance is deallocated, also used in a garbage collected environment.nonatomic
: The accessor is not@synchronized
(threadsafe), and therefore fasteratomic
: The accessor is@synchronized
(threadsafe), and therefore slower
Generally you should always synthesize an ivar. If you need faster access for performance reasons you can always access the synthesized ivar directly too.

Suraj Rao
- 29,388
- 11
- 94
- 103
-
small definitions for all keywords are really helpful...thanks..:) – Pankaj Gupta Feb 20 '12 at 11:04
-
If this helped you should reward Erik by clicking the check mark (tick) next to his answer – David Kennedy of Zenopolis Feb 20 '12 at 11:32
2
While typing I saw that 'Erik Aigner' was faster with a good answer.
For een example on properties, synthesize and custom setter see my answer on stack: Objective-C convention to prevent "local declaration hides instance variable" warning
For an stater tutorial on ARC see the explenation on Ray wenderlich his website: