NSArray 什么时候不再包含相同的项目?
我有一个 NSArray 存储 NSDictionaries。每个字典有两个键;名称和包含“items”的子数组。
我想检查在某个操作之后这些字典之一是否仍然保留在 NSArray 中。在操作之前,我将一个名为“orginalDictionary”的指针存储到我感兴趣的字典中。然后执行我的操作(从该字典内的子数组中删除“项目”之一)。操作后,我想我可以检查字典本身(不是删除的项目)是否仍然存在,使用
if ([arrayOfDictionaries containsObject:originalDictionary]) {...
但它没有按预期工作。即使数组仍然包含原始字典(尽管其中包含修改后的子数组),调用也会返回该数组不再包含该字典。
这样可以吗?如果数组中的某个项目发生更改,containsObject 是否不再将其识别为同一个对象?还是这里发生了其他事情?
编辑 我开始认为数组的填充方式是根本原因。字典数组是从一个或多个其他数组创建的,具体取决于某些设置。 这些数组本身是使用获取请求填充的,该请求根据获取的对象创建字典。因此,当某些数据发生更改时(或者即使只是通过请求数组的返回结果来触发提取),这将重建返回的数组,并且其中的字典将是新项目。这听起来正确吗?如果是这样,我想我别无选择,只能存储自我管理的唯一密钥?
I have an NSArray storing NSDictionaries. Each dictionary has two keys; a name and a sub-array containing 'items'.
I want to check if one of these dictionaries still remains in the NSArray after a certain operation. Before the operation, I store a pointer called 'orginalDictionary' to the dictionary I am interested in. I then perform my operation (which removes one of the 'items' from the sub-array inside that dictionary). After the operation, I thought I could check if the Dictionary itself (not the removed item) still exists, using
if ([arrayOfDictionaries containsObject:originalDictionary]) {...
but it's not working as anticipated. Even though the Array still contains the original dictionary (albeit with a modified sub-array inside it), the call returns that the array no longer contains that dictionary.
Can that be right? If an item in an array changes, does containsObject no longer recognize it as the same object? Or is something else going on here?
EDIT
I'm starting to think that the way in which my array is populated is the root cause. The array of dictionaries is created from one or more other arrays, depending on some settings. These arrays are themselves populated using a fetch request, which created the dictionaries based on the fetched objects. So, when there is a change to some data (or perhaps even if the fetch is fired simply by requesting the return result of the array), this is going to be rebuild the returned array, and the dictionaries inside will be new items. Does this sound about right? If so, I suppose I have no option but to store a self-managed unique key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
containsObject:
确定数组中是否存在表示相同值的对象,这与isEqual:
和indexOfObject: 的测试相同:
使用。例如,包含相同键/值对的两个完全不同的字典将比较相等,因为它们都表示相同的值 - 就像两个整数变量如果包含相同的整数值则比较相等。indexOfObjectIdenticalTo:
方法查找数组中是否存在对象。但这并不能直接解决你的问题。如果您实际上有一个包含 NSMutableDictionary 的 NSMutableArray ,并且您保留了对要检查的 NSMutableDictionary 的引用,则应该进行值或对象比较工作...
要么使用调试器跟踪实际引用(地址/指针),要么使用
NSLog()
和%p
格式将它们转储到控制台(例如NSLog(@"跟踪的字典:%p",originalDictionary)
)。containsObject:
determines whether an object representing the same value is in the array, this is the same test thatisEqual:
andindexOfObject:
use. E.g. two completely different dictionaries which contain the same key/value pairs will compare equal as they both represent the same value - just as two integer variables compare equal if they contain the same integer value.The method
indexOfObjectIdenticalTo:
looks for whether the object is present in the array.However that doesn't directly solve your problem. If you actually have an
NSMutableArray
which containsNSMutableDictionary
s and you keep a reference to theNSMutableDictionary
you're checking for then value or object comparison should work...Either use the debugger to track the actual references (addresses/pointers), or dump them to the console using
NSLog()
and the%p
format (e.g.NSLog(@"Tracked dict: %p", originalDictionary)
).