开发者

Objective-C properties

开发者 https://www.devze.com 2023-03-05 10:10 出处:网络
I was just wondering, when accessing properties from within the class, do I have to do [self someProperty] or self.someProperty? Or is it safe to refer to it simply as someProper开发者_如何学Pythonty?

I was just wondering, when accessing properties from within the class, do I have to do [self someProperty] or self.someProperty? Or is it safe to refer to it simply as someProper开发者_如何学Pythonty?

EDIT: Given that the name of the property doesn't conflict/shawdow....


Either will work. The .someProperty notation is arguably a little safer, as the compiler will throw an error if you make a typo there; by contrast, a mistyped [self someProperty] will only generate a warning.


It's fine to refer to the instance variable using just someProperty when reading the value, but when setting the value, if you aren't using [self setSomeProperty:] or self.someProperty =, then there will by no key-value observing messages sent. So if any other object is observing this property, they won't be updated correctly.

This can, occasionally, be what you want by setting the instance variable directly, but usually you want observing to work.


[self someProperty] or self.someProperty? or can is it safe to refer to it simply as someProperty?

Any one is fine. But I prefer not to use self when being in scope of class.

someValue = 10 ;
[ self someValue ] = 20 ;
self.someValue = 30 ;

All the above three statements are going the modify the interface variable someValue.

0

精彩评论

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

关注公众号