Question

I see many keypaths being specified as self.someProperty instead of just someProperty. Is there a benefit to doing this? Are the two keypaths equivalent?

Edit: I am talking about keypath strings, for example used in bindings, sent to observeValueForKeyPath:, or returned from keyPathsForValuesAffectingValueForKey:

Was it helpful?

Solution

Do you mean with valueForKey:/setValue:forKey: or just directly as per the difference between:

var = 8;
self.var = 8;

Assuming the latter, the difference is that the former stores the value directly, C style, whereas the latter calls the setter (or getter, depending on context).

Calling the method costs basically nothing to take that issue off the table but has the advantages that:

  • the property is key-value observing compliant; and
  • you can add whatever other processing logic you want to the set. But more than that: so can subclasses, so your class becomes a better citizen in a large project.

EDIT: if the former then, no, there are no behavioural advantages but I guess that using key-value coding on yourself is such an unusual thing to do there's a benefit it making it look unusual?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top