Wednesday, March 21, 2012

Know which one is accessed in objective C: Instance variable or property

It may create some subtle bugs if you do not pay attention on which one is accessed inside a objective c method, as property and instance variable have the same name.
If you access from outside, then you know you are accessing the property as instance variables can not be accessed from outside of the class instance methods.
However if you access the variable or property by name from inside the class, it really depends on whether the key word "self" is used or not.
If you access through self, like self.myVariable, then the property is used and most likely, it is also retained.
If you access through the variable alone, then the instance variable is used and no property related feature, like retain, will apply.
So if next time you ios app crashes, pay attention to which one you are access inside your member method.