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]];

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]];
链接地址: http://www.djcxy.com/p/8304.html

上一篇: Boost中是否有容器外观?

下一篇: NSMutableDictionary添加和删除KVO