What is the difference between these declarations, lets call them red and orange: pros, cons?
2 Answers
The red set is properties, and the orange set is instance variables.
A property declaration tells the compiler to define a getter method, and possibly a setter method. (No setter method if the property is readonly.)
In newer versions of Objective C, declaring a property also creates an instance variable that is used to save values for the property. By convention the instance variable has the same name as the property, but with an "_" prefix. There is a way to change the name of the instance variable, but let's ignore that for now.
The property foo:
@property (nonatomic, strong) NSString *foo;
Would have a getter method:
- (NSString *) foo;
and a setter method
- (void) setFoo: (NSString *) foo;
That enables you to use code like this:
NSString *aString = self.foo;
or
NSString *aString = [self foo];
(2 different, equally valid ways of invoking the getter)
And invoking the setter
self.foo = @"a string";
or
[self setFoo: @"a string"];
(2 different, equally valid ways of invoking the setter)
Properties are really useful when you want to create a public interface to get and set values in your class from outside. If you declare a property as "atomic" the compiler adds additional code to the getter and the setter so reads and writes to the property are "thread safe", and can be accessed from background threads.
Before ARC, properties also were a very clean way to manage retains and releases. You declared a property as "retain" and the setter was written to retain the object that was passed in. That's less of an issue in ARC, because the system takes care of retains and releases for you.
It is also possible to write a custom getter or setter method that invokes your own code instead of the compiler-written code. You can use that to do things like log information, send notifications about changes, update labels, etc, etc. You simply add a method body to your .m file that has the same method signature as the getter or setter and the compiler uses that method instead of the automatically generated one.
As I said before, the code:
self.foo = @"a string";
is the same as
[self setFoo: @"a string"];
and invokes the setter method. The setter method sets the internal instance variable _foo.
However, the code
_foo = @"a string";
changes the instance variable directly, without invoking the setter. If you do define a property, you should use it instead of the instance variable.

- 128,072
- 22
- 173
- 272
-
Can you explain or give one simaple example where you can explain the last line ? Where it will make a difference in changing the instance variable without invoking the setter. – Bishal Ghimire Sep 07 '13 at 23:57
-
1@BishalGhimire If you have a custom implementation of the setter, setting the ivar directly will bypass that extra functionality. This may or may be not be desirable for a given situation. You also miss out on using KVO when you set the ivar directly instead of using the property. – rmaddy Sep 08 '13 at 01:12
Objective-c use to be simple...and tedious. You would declare instance variables for a class (the orange) and then you would define (usually) 2 methods for each, one so that an external class could set each instance variable to a new value, and one that returned the ivars value so an external object could read it. Aka, you had to write a getter and setter for each ivar. This was two lines of code in the interface and sometimes around 10 lines of code for the implementation file.
Then came properties, declared with @property. There was much rejoicing and drinking in the streets. These single @property lines told the compiler to write those methods for you, including the correct memory management code and even mutex locking code (depending on what you specified when declaring the @property)
Theres a whole lot of history, but nowadays, with automatic reference counting, it really only makes sense to use @properties in your interface, when you want to make an ivar public, and declare your private ivars in your implementation file.
Lastly, not that @property not only tells the compiler to implement your getter and setter, but it also automatically provides an instance variable with the same name, but prefixed with an underscore (this is only if you have implicit synthesizing of properies enabled...more history)
So, thats the difference. @property tells the compiler to write code for you (essentially). What code it actually writes is modified by all the different ways you can declare a @property.

- 3,701
- 27
- 30
-
1+1 for saying "only makes sense to use @properties in your interface," (with ARC). I still don't know why people keep using private properties nowadays – Lucas Eduardo Sep 07 '13 at 23:16
-
@LucasEduardo Properties make working with KVO much easier. That is one of the key benefits to private ivars. It also provide proper encapsulation allowing for things like lazy-loading, etc. – rmaddy Sep 08 '13 at 01:10
-
I do know the benefits, but I think they are circumstantial in general. I just don't like to use private properties (!= iVars) UNLESS you have a good reason to. – Lucas Eduardo Sep 08 '13 at 02:42