I know it's often advisable to use the @property
accessors, and I know it's important to use self.myView = x
inst开发者_运维技巧ead of mView = x
, but:
Isn't
myView.frame
sufficient? The AtomicElementFlippedView.m file in TheElements hasself.wikipediaButton.frame=buttonFrame;
. Shouldn't I leave outself
if I'm getting/setting a property of my ivar and not the ivar itself?It also does
[self.wikipediaButton addTarget:...];
. But shouldn't I leave outself
here as well? Shouldn't I always call a function on the ivar, not the property? I.e.,[wikipediaButton addTarget:...];
Thanks!
Matt
Both will work.
The main reason for using getters within a class is that code changes are easy. If you decide to store your data in a different fashion or to build it dynamically on request, there is a single point of change. With views it's not that important most of the time, but it can come handy when you replace that view with a complete view hierarchy (your original view being maybe part of it).
I'd say the main reason you'd want to use the property setter instead of direct ivar access is simply consistency. Yes, you can access the ivar directly, and you'll usually be just fine doing so. However, I'd say it's stylistically better to use the same method for both setting and getting. So if you're using property setters internally, you should also use property getters internally.
精彩评论