开发者

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 unders开发者_如何学Pythontand 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]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜