【发布时间】:2011-02-25 20:42:27
【问题描述】:
我有一个关于 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");
}
我想上述检查会给我一个积极的结果,但事实并非如此。此外,我检查了创建的对象是否相同。
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);
}
在控制台中,这是我得到的:
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 位于collection 数组中,但是当我使用containsObject: 检查它时,什么也没有出现。谁能给我一些指导,我错了哪一部分?非常感谢!
【问题讨论】:
标签: iphone arrays xcode compare