I have a quick question regarding KVO. I understand that for a NSArray if observing additions is desired you can do the following.
NSIndexSet* set = [NSIndexSet indexSetWithIndex:[someIndex integerValue]];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:set forKey:@"theArrayName"];
// add the objects at the specified indexes here
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:set forKey:@"theArrayName"];
Upon registering for observation you will get a change dictionary with the indexes that changed.
However, I do not understand how I can observe new object additions to a NSMutableDictionary. Is there a way to observe object add/removal?
Thanks,
[EDIT] I found a solution which meets my current needs. I hope the following helps any future developers.
[self willChangeValueForKey:@"someDictionary" withSetMutation:NSKeyValueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];
[Images setObject:someObject forKey:someIndex];
[self didChangeValueForKey:@"someDictionary" withSetMutation:NSKeyValueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];
Just to make sure that question is answered. credit goes to @DoubleDunk
[self willChangeValueForKey:@"someDictionary" withSetMutation:NSKeyValueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];
[Images setObject:someObject forKey:someIndex];
[self didChangeValueForKey:@"someDictionary" withSetMutation:NSKeyValueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With