user635064
user635064

Reputation: 6247

Objective-C properties

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 someProperty?

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

Upvotes: 0

Views: 170

Answers (3)

Greg Titus
Greg Titus

Reputation: 461

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.

Upvotes: 1

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

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.

Upvotes: 2

Mahesh
Mahesh

Reputation: 34625

[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.

Upvotes: 0

Related Questions