Yossi
Yossi

Reputation: 2535

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

Upvotes: 8

Views: 2985

Answers (2)

Omarj
Omarj

Reputation: 1159

Try to do this

(lldb) p (NSUInteger)[self.items indexOfObject:newItems];

Upvotes: 1

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16660

-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.

Upvotes: 16

Related Questions