Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableDictionary addition and removal KVO

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]];
like image 441
DoubleDunk Avatar asked Jul 05 '11 16:07

DoubleDunk


1 Answers

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]];
like image 73
fibnochi Avatar answered Oct 22 '22 14:10

fibnochi