I have a quick question regarding KVO. I understand that for NSArray, if you want to watch the additions, you can do the following.
NSIndexSet* set = [NSIndexSet indexSetWithIndex:[someIndex integerValue]]; [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:set forKey:@"theArrayName"];
After registering for observation, you will receive a dictionary of changes with modified indexes.
However, I do not understand how I can observe the addition of new objects to NSMutableDictionary. Is there a way to watch for adding / removing an object?
Thanks,
[EDIT] I found a solution that meets my current needs. I hope the following will help 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]];
Doubledunk
source share