Properties and Instance Variables in Objective-C

In the iPhone world, there’s no garbage collector available. You’ll have to carefully manage memory with reference counting. With that in mind, consider the difference between:

name = @"Test";

and

self.name = @"Test";
// which is equivalent to:
[self setName: @"Test"];

If you directly set the instance variable, without prior consideration, you’ll lose the reference to the previous value and you can’t adjust its retain count (you should have released it manually). If you access it through a property, it’ll be handled automatically for you, along with incrementing the retain count of the newly assigned object.

The fundamental concept is not iPhone specific but it becomes crucial in an environment without the garbage collector.

Leave a Comment