KVC setNilValueForKey: recommends calling method and not using accessor property

KVC documentation says

SetNilValueForKey key value encoding method: the method is called when trying to set the attribute to nil.

Sounds good still

... uses setValue: forKey: to set a new value. This supports the encapsulation of the model and ensures that any additional actions that must occur as a result of setting the value do occur. This is considered best practice than invoking an access method or setting an instance variable directly.

Why is it better to use the -setValue:forKey: method inside the -setNilValueForKey: method when setting the default value for a primitive property or value type? Is there a performance or technical advantage for using the KVC -setValue:forKey: method, which is the opposite of the property access object (I assume that when it talks about the access method, it also applies to access properties, since they are just syntactic sugar over the method) ? Typically, when Apple recommends “best practices," there is a performance or reliability issue. Does anyone know a documentary reason?

+4
source share
1 answer

From your quote:

This supports the encapsulation of the model and ensures that any additional actions that must occur as a result of setting the value are valid.

Calling setValue:forKey: instead of accessories or changing ivar ensures that all appropriate side effects are preserved. When the citation is mentioned in the citation, it means staying in KVC methods instead of custom accessories. Calling setValue:forKey: also means that you get a runtime to determine how the property should be configured for you. Finally, the “complementary actions” probably refer to observing key values. He will check the correct methods, not any that should not be called.

+3
source

All Articles