Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSArray indexOfObject returning nil

Any idea why can't I get the index of an object that I'm sure is exist in the array? Instead, I'm getting nil..

(lldb) po newItem
<ReceiptItem: 0x16a428b0>

(lldb) po self.items
<__NSArrayM 0x169bf0e0>(
<ReceiptItem: 0x16a428b0>
)

(lldb) po [self.items indexOfObject:newItem]
<nil>

Thanks

like image 394
Yossi Avatar asked May 31 '15 09:05

Yossi


2 Answers

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

It returns 0, not nil, what means that it found the object at the first position of the array. If it would not find the object, -indexOfObject: would return NSNotFound.

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

like image 150
Amin Negm-Awad Avatar answered Nov 16 '22 17:11

Amin Negm-Awad


Try to do this

(lldb) p (NSUInteger)[self.items indexOfObject:newItems];
like image 41
Omarj Avatar answered Nov 16 '22 17:11

Omarj