NSArray containObjects method
Posted
by Anthony Chan
on Stack Overflow
See other posts from Stack Overflow
or by Anthony Chan
Published on 2010-05-31T05:52:46Z
Indexed on
2010/05/31
6:02 UTC
Read the original article
Hit count: 262
Hi, 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]; //custom "Item" objects
Item *tempItem = [[Fruit 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!
© Stack Overflow or respective owner