开发者

myView.frame Vs. self.myView.frame

开发者 https://www.devze.com 2023-01-18 02:21 出处:网络
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:

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:

  1. Isn't myView.frame sufficient? The AtomicElementFlippedView.m file in TheElements has self.wikipediaButton.frame=buttonFrame;. Shouldn't I leave out self if I'm getting/setting a property of my ivar and not the ivar itself?

  2. It also does [self.wikipediaButton addTarget:...];. But shouldn't I leave out self 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消