ios - NSArray indexOfObject 返回 nil

标签 ios objective-c nsarray

知道为什么我不能获得我确定存在于数组中的对象的索引吗?相反,我得到了 nil..

(lldb) po newItem
<ReceiptItem: 0x16a428b0>

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

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

谢谢

最佳答案

-indexOfObject: 返回类型为 NSUInteger 的整数,而不是对象引用。因此,您不应使用 po(打印对象)调试器命令,而应使用 p

它返回0,而不是nil,这意味着它在数组的第一个位置找到了对象。如果找不到对象,-indexOfObject: 将返回 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.

关于ios - NSArray indexOfObject 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30555966/

相关文章:

objective-c - UITableView 动画令人头疼

ios - 通过第二个、第三个等单词的锚定搜索对字符串数组进行排序

ios - 如何从用户定义方法返回到委托(delegate)方法

ios - Google Plus 使用 OAuth 2.0 登录滚动问题

ios - 如何在 iOS 中使用本地化语言更改 xib

objective-c - iOS 将文本输入限制为一组语言

iphone - NSZombieEnabled 防止我的​​应用程序崩溃

objective-c - 检查 NSArray 中的所有对象是否相同

ios - "Share on facebook"打开我的应用程序

iphone - 使用 "User Defined Runtime Attributes"在 Storyboard中本地化 View