Reputation: 40329
This may be a really silly beginner question, BUT:
If you have some nice instance variables in your class, such as an UIScrollview *scrollView2 for example, then why should you bypass the getter and setter by relinquishing a
[self.scrollView2 addSubview:imageView];
, and rather doing a
[scrollView2 addSubview:imageView];
? I mean...going over the getter doesn't hurt, and actually I thought that's always the way to go. But in all the Apple examples I miss that pattern all over the place. They rarely use self.someInstanceVariable when they invoke methods. Or did I get something wrong?
I started to do the same thing since apple does it, but I'd like to know: Why?
Upvotes: 0
Views: 135
Reputation: 40507
Typically you wouldn't use properties in init
or dealloc
, it's best to access the instance variables directly since your object might not be in a "safe" state.
Upvotes: 1
Reputation: 25665
It has to do with KVO (Key-Value Observing) and KVC (Key-Value Coding). If you bypass the getter or setter, objects watching the value won't get the message.
Upvotes: 3
Reputation: 29842
I used to bypass the getter/setter too, until I figured out that the bindings doesn't work when directly accessing the variable. In other words, key-value coding only works when accessing a property with the getter/setter functions.
EDIT: See this page for the KVC compliance.
Upvotes: 2