Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a couple of reasons why the notification for <code>count</code> isn't firing, both having to do with Key-Value Coding.</p> <p>The first reason is because the <code>count</code> property is not updated when calling <code>addTask:</code>. There isn't a direct link between the <code>count</code> property and the count of the _tasks array. Any code manipulating the array has to be wrapped between <code>-willChangeValueForKey:</code> and <code>-didChangeValueForKey:</code> calls. However, the KVO protocol provides the ability to set dependent keys. Since the count property is affected by the array, you can set a dependency between these two keys by using</p> <pre><code>+ (NSSet*) keyPathsForValuesAffectingCount { return [NSSet setWithObject:@"tasks"]; } </code></pre> <p>Or the more generic <code>+ keyPathsForValuesAffectingValueForKey:(NSString *)key</code>. Implementing either will fire notifications for <code>count</code> when <code>tasks</code> is modified.</p> <p>The second reason why this won't work, is because you're not updating the <code>tasks</code> array in a Key-Value compliant way as defined in the <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/AccessorConventions.html#//apple_ref/doc/uid/20002174-BAJEAIEE" rel="nofollow">Key-Value Coding Programming Guide</a>. Basically, objects in ordered collections have to be added with either</p> <pre><code>-insertObject:in&lt;Key&gt;AtIndex: -insert&lt;Key&gt;:atIndexes: </code></pre> <p>Or be wrapped with the <code>will|didChangeValueForKey:</code> calls.</p> <p>In your example, if you provide the following implementation after setting the key dependency, you should observe a change.</p> <pre><code>- (void)addTask:(JHTask *)task { [self insertObject:task inTasksAtIndex:[_tasks count]]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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