ios – NSMutableDictionary添加和删除KVO

我有一个关于KVO的快速问题.我理解,对于NSArray,如果需要观察添加,您可以执行以下操作.
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"];

注册观察后,您将获得一个更改字典,其中包含已更改的索引.

但是,我不明白如何观察NSMutableDictionary的新对象添加.有没有办法观察对象的添加/删除

谢谢,

[编辑]
我找到了满足当前需求的解决方案.我希望以下内容有助于未来的开发人员.

[self willChangeValueForKey:@"someDictionary" withSetMutation:NSkeyvalueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];

[Images setobject:someObject forKey:someIndex];

[self didChangeValueForKey:@"someDictionary" withSetMutation:NSkeyvalueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];

解决方法

只是为了确保回答问题.信用转到@DoubleDunk
[self willChangeValueForKey:@"someDictionary" withSetMutation:NSkeyvalueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];

    [Images setobject:someObject forKey:someIndex];

    [self didChangeValueForKey:@"someDictionary" withSetMutation:NSkeyvalueUnionSetMutation usingObjects:[NSSet setWithObject:someObject]];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...