Adding and Removing NSMutableDictionary KVO

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"]; // add the objects at the specified indexes here [self didChange: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]]; 
+7
source share
1 answer

Just to make sure the question is answered. loan 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]]; 
+3
source

All Articles