对于复制的对象可变数组比较 [英] Mutable array comparison for copied objects

查看:259
本文介绍了对于复制的对象可变数组比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做2可变数组的数组进行比较。在阵列中的一个,我存储我的模型对象和其他数组中我使用的存储模型对象的副本 [myObject的副本] 。我的模型对象是 NSObject的的一个子类,所以我已经添加了 copyWithZone:方法为好。然而,当我做阵列比较使用 isEqualToArray 这两个数组总是返回false。将比较不复制的对象工作?还是我走错了别的地方?

P.S :作为一个概述,我想要做的是检查在我的模型东西是否改变调用更新服务之前。我想打电话给只有当所有的模型对象已经改变了服务。


解决方案

  

请问比较行不通上复制的对象?


您可以很容易地找到,只需复制单个对象和检查平等agains原有的回答这个问题。

扰流

您会看到将取决于如果您已实现自定义的的isEqual结果:的方法你的班。如果没有这些,它会默认为落实超(NSObject的),它认为平等是相同的指针。由于副本是一个新的指向同一个对象,NSObject的不会考虑他们平等的。

我会建议您阅读有关此NSHipster文章rel=\"nofollow\">在和/或本文由麦克·阿什(如果你感觉好奇)

I am trying to do array comparison on 2 mutable arrays. In one of the array, i am storing my model objects and in the other array I am storing a copy of the model objects using [myObject copy]. My model object is a subclass of NSObject so I have added the copyWithZone: method as well. However when I do array compare using isEqualToArray on these two arrays it always returns false. Will the compare not work on copied objects? Or am I going wrong somewhere else?

P.S: As an overview, what I'm trying to do is to check whether something is changed in my model before calling an update service. I want to call the service Only if any of the model objects have changed.

解决方案

Will the compare not work on copied objects?

You can very easily find out the answer to this question by just copying a single object and checking for equality agains the original.

SPOILER

The results you are going to see will depend on if you have implemented custom hash and isEqual: methods in your class. Without those it will default to the superclasses implementation (NSObject) which considers equality to be the same pointer. Since a copy is a new pointer to the same object, NSObject won't consider them equal.

I would recommend that you read about object equality in this NSHipster article (great to start with) and/or in this article by Mike Ash (if you are feeling curious)

这篇关于对于复制的对象可变数组比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆