xcode NSArray containsObject 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2941596/
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
NSArray containsObject method
提问by Anthony
I have a simple question regarding xcode coding but don't know why things are not performing as I think. I have an array of objects (custom objects). I just want to check if this one is within the array. I used the following code:
我有一个关于 xcode 编码的简单问题,但不知道为什么事情没有像我想象的那样执行。我有一组对象(自定义对象)。我只想检查这个是否在数组中。我使用了以下代码:
NSArray *collection = [[NSArray alloc] initWithObjects:A, B, C, nil]; //A, B, C are custom "Item" objects
Item *tempItem = [[Item alloc] initWithLength:1 width:2 height:3]; //3 instance variables in "Item" objects
if([collection containsObject:tempItem]) {
NSLog(@"collection contains this item");
}
I suppose the above checking will give me a positive result but it's not. Further, I checked whether the objects created are the same.
我想上述检查会给我一个积极的结果,但事实并非如此。此外,我检查了创建的对象是否相同。
NSLog(@"L:%i W:%i H:%i", itemToCheck.length, itemToCheck.width, itemToCheck.height);
for (int i = 0, i < [collection count], i++) {
Item *itemInArray = [collection objectAtIndex:i];
NSLog(@"collection contains L:%i W:%i H:%i", itemInArray.length, itemInArray.width, itemInArrayheight);
}
In the console, this is what I got:
在控制台中,这是我得到的:
L:1 W:2 H:3
collection contains L:0 W:0 H:0
collection contains L:1 W:2 H:3
collection contains L:6 W:8 H:2
Obviously the tempItem
is inside the collection
array but nothing shows up when I use containsObject:
to check it. Could anyone give me some direction which part I am wrong? Thanks a lot!
显然tempItem
是在collection
数组内,但当我containsObject:
用来检查它时什么也没显示。谁能给我一些指导,我错了哪一部分?非常感谢!
回答by Senseful
The documentation for [NSArray containsObject:]
says:
的文档[NSArray containsObject:]
说:
This method determines whether anObject is present in the receiver by sending an isEqual: message to each of the receiver's objects (and passing anObject as the parameter to each isEqual: message).
此方法通过向每个接收器的对象发送 isEqual: 消息(并将 anObject 作为参数传递给每个 isEqual: 消息)来确定接收器中是否存在对象。
The problem is that you are comparing references to objects rather than the values of the objects. To make this specific example work, you will either need to send [collection containsObject:]
an instance of a variable it contains (e.g. A
, B
, or C
), or you will need to override the [NSObject isEqual:]
method in your Item
class.
问题在于您正在比较对对象的引用而不是对象的值。为了使这个特定示例工作,您将需要发送[collection containsObject:]
它包含的变量的实例(例如A
、B
、 或C
),或者您需要覆盖类中的[NSObject isEqual:]
方法Item
。
This is what your isEqual
method might look like:
您的isEqual
方法可能如下所示:
- (BOOL)isEqual:(id)other {
if (other == self)
return YES;
if (!other || ![other isKindOfClass:[self class]])
return NO;
if (self.length != other.length || self.width != other.width || self.height != other.height)
return NO;
return YES;
}
For a better implementation, you may want to look at this question.
为了更好的实现,你可能想看看这个问题。