相关疑难解决方法(0)

NSArray containsObject方法

我有一个关于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");
}
Run Code Online (Sandbox Code Playgroud)

我想上面的检查会给我一个积极的结果,但事实并非如此.此外,我检查了创建的对象是否相同.

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);
}
Run Code Online (Sandbox Code Playgroud)

在控制台中,这是我得到的:

L:1 W:2 H:3
collection contains L:0 W:0 H:0 …
Run Code Online (Sandbox Code Playgroud)

arrays iphone xcode compare

17
推荐指数
1
解决办法
3万
查看次数

标签 统计

arrays ×1

compare ×1

iphone ×1

xcode ×1