문제

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:

도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top