views:

81

answers:

3

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?

+2  A: 

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.

Martin Cote
+2  A: 

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.

rjstelling
okay so in these cases Apple just wanted to make sure that no one else will get notified or whatever if they use an instance variable? How about all the memory management stuff that's done by the accessors?
Thanks
Swanzus Longus: One possible reason is that nothing was observing that property, so it didn't really matter and was slightly faster without the accessor message (bad reason—always use the accessor message on any public property, just in case you start observing it in the future). Another possible reason is that it isn't a public property of the object: it's just an ivar, nothing more.
Peter Hosey
Thanks Peter. So is it valid to say: "If you have an public property, never bypass the accessors"? I mean... for me it makes just no sense to bypass them. This one little accessor call shouldn't delay things so much...Except in an environment where some loop attacks an instance variable 1000 times a second.
Thanks
Just another note after trying out some stuff: If you (anyone) bypass accessors by not using "self.", then at least don't do it when you really change the value of the instance variable itself. i.e. if you assign some new object to it. But if you just send a message to an object where the instance variable points to, then the accessors don't matter much anyway. Well, at least when it comes to memory management. i.e. if you bypass an instance variable that has @property(nonatomic, retain), nothing will get retained when you assign a new object to it. So I'll just not bypass them.
Thanks
+1  A: 

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.

Marc Charbonneau
I guess with properties you mean the usage of accessors, right?
Thanks
Right. Properties was probably the wrong word, the same is true if you wrote your own accessor methods without declaring it a property.
Marc Charbonneau