objective-c 如何比较两个对象是否真的是同一个对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1319247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare if two objects are really the same object?
提问by Peter Hosey
I want to compare if an variable A represents the same object as variable B does.
我想比较变量 A 是否与变量 B 代表相同的对象。
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?
或者这到底是在看什么?我想我需要检查变量指向的对象的内存地址,对吗?
回答by Peter Hosey
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:).
该==运算符测试两个表达式是否是指向同一对象的同一指针。Cocoa 称这种关系为“相同的”(例如,参见 NSArray 的indexOfObjectIdenticalTo:)。
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 YESif 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.
要测试两个对象是否相等,您可以向其中一个对象发送一条isEqual:消息(或更具体的消息,例如isEqualToString:,如果它响应一个),然后传递另一个对象。YES如果你真的只有一个对象(显然等于它自己)或者如果你有两个相等的对象,这将返回。在后一种情况下,==将评估为NO。
回答by nevan king
The == tells you if two pointers are pointing to the same object. isEqualtells you if the contents of two objects are the same (but not necessarily the actual same object). A little confusing.
== 告诉你两个指针是否指向同一个对象。isEqual告诉您两个对象的内容是否相同(但不一定是实际的相同对象)。有点混乱。
Try this code to understand it better:
试试这个代码以更好地理解它:
NSString *aString = [NSString stringWithFormat:@"Hello"];
NSString *bString = aString;
NSString *cString = [NSString stringWithFormat:@"Hello"];
if (aString == bString)
NSLog(@"CHECK 1");
if (bString == cString)
NSLog(@"CHECK 2");
if ([aString isEqual:bString])
NSLog(@"CHECK 3");
if ([bString isEqual:cString])
NSLog(@"CHECK 4");
// Look at the pointers:
NSLog(@"%p", aString);
NSLog(@"%p", bString);
NSLog(@"%p", cString);
回答by Williham Totland
[objectA isEqual:objectB]is usually a good choice. Note that some classes may have more specialized equality functions. (isEqualToString:et.al.) These generally test not if they are the same object, but if the objects are equal, which is a distinct concept. (Two string objects can be equal, even if they don't have the same memory address.)
[objectA isEqual:objectB]通常是一个不错的选择。请注意,某些类可能具有更专门的相等函数。( isEqualToString:et.al.) 这些通常不是测试它们是否是同一个对象,而是测试对象是否相等,这是一个不同的概念。(两个字符串对象可以相等,即使它们没有相同的内存地址。)
回答by Jens Ayton
The two other answers correctly answer the question in your title. The correct answer to the completely different question in your body text, however, is: yes, the == operator is correct for testing whether two variablesrefer to the same object.
另外两个答案正确回答了标题中的问题。然而,对正文中完全不同的问题的正确答案是:是的,== 运算符对于测试两个变量是否指向同一个object是正确的。

