NSArray indexOfObject returns zero

Any idea why I cannot get the index of an object that I am sure exists in the array? Instead, I get zero.

(lldb) po newItem <ReceiptItem: 0x16a428b0> (lldb) po self.items <__NSArrayM 0x169bf0e0>( <ReceiptItem: 0x16a428b0> ) (lldb) po [self.items indexOfObject:newItem] <nil> 

thanks

+7
ios objective-c nsarray
source share
2 answers

-indexOfObject: returns an integer of type NSUInteger , not an object reference. Therefore, you should not use the debug command po (print object), but p .

It returns 0 , not nil , which means that it found the object in the first position of the array. If it does not find the object, -indexOfObject: will return NSNotFound .

The lowest index whose corresponding array value is aObject. If none of the objects in the array is equal to anObject, returns NSNotFound.

+12
source share

Try to do it

 (lldb) p (NSUInteger)[self.items indexOfObject:newItems]; 
+1
source share

All Articles