NSArray的containsObject方法 [英] NSArray containsObject method
问题描述
我有作为X code编码一个简单的问题,但不知道为什么事情都没有执行,因为我认为。我有对象(自定义对象)的数组。我只是想检查,如果这个人是在阵列内。我用下面的code:
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:
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
显然 tempItem
是内部集合
阵列,但没有显示出来,当我使用 containsObject:
进行检查。任何人都可以给我一些指导,我哪一部分错了吗?非常感谢!
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!
推荐答案
有关文档<一个href=\"http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/containsObject:\"相对=nofollow> [NSArray的containsObject:]
说:
The documentation for [NSArray containsObject:]
says:
此方法确定是否
anObject是通过在接收机present
发送isEqual:方法判断消息给每个
接收器的对象(和传球
anObject作为参数到每个
isEqual:方法:消息)
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).
问题是,你要比较的对象引用,而不是对象的值。为了使这个具体的例子工作,你要么需要发送 [收藏containsObject:]
它包含一个变量的一个实例(如 A
, B
或 C
),否则你将需要重写<一个href=\"http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isEqual:\"相对=nofollow> [NSObject的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.
这是你的的isEqual
方法可能是什么样子:
This is what your isEqual
method might look like:
- (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;
}
为了更好的实现,你可能想看看这个<一个href=\"http://stackoverflow.com/questions/254281/best-practices-for-overriding-isequal-and-hash\">question.
这篇关于NSArray的containsObject方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!