Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>But shouldn't the synthesized accessors automatically return such a proxy object?</p> </blockquote> <p>No.</p> <blockquote> <p>What's the proper way to work around this--should I write a custom accessor that just invokes <code>[super mutableArrayValueForKey...]</code>?</p> </blockquote> <p>No. Implement the <a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/DefiningCollectionMethods.html#//apple_ref/doc/uid/10000107i-CH17-SW1" rel="nofollow noreferrer">array accessors</a>. When you call these, KVO will post the appropriate notifications automatically. So all you have to do is:</p> <pre><code>[myObject insertObject:newObject inTheArrayAtIndex:[myObject countOfTheArray]]; </code></pre> <p>and the Right Thing will happen automatically.</p> <p>For convenience, you can write an <code>addTheArrayObject:</code> accessor. This accessor would call one of the real array accessors described above:</p> <pre><code>- (void) addTheArrayObject:(NSObject *) newObject { [self insertObject:newObject inTheArrayAtIndex:[self countOfTheArray]]; } </code></pre> <p>(You can and should fill in the proper class for the objects in the array, in place of <code>NSObject</code>.)</p> <p>Then, instead of <code>[myObject insertObject:…]</code>, you write <code>[myObject addTheArrayObject:newObject]</code>.</p> <p>Sadly, <code>add&lt;Key&gt;Object:</code> and its counterpart <code>remove&lt;Key&gt;Object:</code> are, last I checked, only recognized by KVO for set (as in NSSet) properties, not array properties, so you don't get free KVO notifications with them unless you implement them on top of accessors it does recognize. I filed a bug about this: x-radar://problem/6407437</p> <p>I have <a href="http://boredzo.org/blog/archives/2007-08-07/a-complete-raw-list-of-kvc-accessor-selector-formats" rel="nofollow noreferrer">a list of all the accessor selector formats</a> on my blog.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload