Search code examples
objective-cpropertieskey-value-observing

Can Custom Setters Break KVO Pattern? Break Retain or Copy Pattern?


In Objective-C, I wonder whether a custom setter will override the KVO support (willChangeValueForKey: and didChangeValueForKey;) and do I need to include calls to these explicitly in the custom setter?

What about Retain and Copy? Would I need to explcitly include release and retain or copy in the setter for properties with Retain or Copy attributes (for non-ARC code)?


Solution

  • KVO will work automatically if your accessors are in the standard format (-setFoo: and -foo). Retain and copy, however, you will need to manage yourself in your custom accessors.

    This is because retain and copy are part of the synthesized accessors, whereas KVO is based solely on method names. Check here for a full description of KVO-compliant method naming.