微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/iOS/331276.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐