gpt4 book ai didi

ios - Objective C - NSArray 的 indexOfObject : not work

转载 作者:行者123 更新时间:2023-12-01 18:37:13 25 4
gpt4 key购买 nike

我用 [items indexOfObject:items.lastObject]获取最后一个索引,但此代码返回 nil .为什么会这样?
enter image description here

最佳答案

数组中的第一个和最后一个对象都是使用“固定空间”系统项创建的条形按钮项。

调用 indexOfObject: 的结果是 0 ,而不是 nil .这意味着在索引 0 处找到对象。 . indexOfObject:无法返回 nil .如果未找到对象,则返回特殊值 NSNotFound这是 -1 的无符号值.

来自 indexOfObject: 的文档:

Starting at index 0, each element of the array is passed as an argument to an isEqual: message sent to anObject until a match is found or the end of the array is reached. Objects are considered equal if isEqual: (declared in the NSObject protocol) returns YES.


UIBarButtonItem isEqual:的执行将返回 YES如果使用相同的系统项(可能还有一些其他属性)创建了两个条形按钮项实例。
indexOfObject:不是基于对象的实例,而是基于 isEqual: .

如果要根据对象的标识(其地址)而不是 isEqual:来查找对象的索引, 使用 indexOfObjectIdenticalTo: .
p [items indexOfObjectIdenticalTo:items.lastObject]

会给你 6而不是 0 .

关于ios - Objective C - NSArray 的 indexOfObject : not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51034143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com