如何比较两个对象是否真的是同一个对象? [英] How to compare if two objects are really the same object?

查看:182
本文介绍了如何比较两个对象是否真的是同一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较变量A是否代表与变量B相同的对象。

I want to compare if an variable A represents the same object as variable B does.

我可以用==运算符吗?

Could I do that with the == operator?

还有什么是这正在看?我想我需要检查变量指向的对象的内存地址,对吗?

Or what else is this exactly looking at? I think I need to check for the memory adress of the object where the variable is pointing to, right?

推荐答案

c $ c> == 运算符测试这两个表达式是否是同一个对象的同一个指针。 Cocoa调用这个关系相同(例如,参见NSArray的 indexOfObjectIdenticalTo:)。

The == operator tests whether the two expressions are the same pointer to the same object. Cocoa calls this relation "identical" (see, for example, NSArray's indexOfObjectIdenticalTo:).

对象是相等的,你会发送一个 isEqual:消息(或更具体的消息,如 isEqualToString:,如果它响应一个),传递另一个对象。如果你真的只有一个对象(显然等于自身)或者你有两个相等的对象,这将返回 YES 。在后一种情况下, == 将计算为 NO

To test whether two objects are equal, you would send one of them an isEqual: message (or a more specific message, such as isEqualToString:, if it responds to one), passing the other object. This would return YES if you really only have one object (equal to itself, obviously) or if you have two objects that are equal. In the latter case, == will evaluate to NO.

这篇关于如何比较两个对象是否真的是同一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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