objective-c - indexOfObjectIdenticalTo : returns value outside of array count

标签 objective-c ios uitableview indexing nsmutablearray

我目前正在开发一个填充 UITableView 的应用程序。来自 MPMediaItemCollection 的项目.我正在尝试添加 UITableViewCellAccessoryCheckmark到与当前播放轨道的标题匹配的行。

我已经通过创建一个可变的轨道标题数组来做到这一点,这也是为我的单元格的 textLabel.text 设置的。属性(property)。 (用于比较目的)

注:这一切都在 - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 中完成

MPMediaItem *mediaItem = (MPMediaItem *)[collectionMutableCopy objectAtIndex: row];

if (mediaItem) {
    cell.textLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyTitle];
}



[mutArray insertObject:cell.textLabel.text atIndex:indexPath.row];

据我所知,这一切都很好,除了以下内容。此时,我正在尝试获取当前播放轨道标题的索引并添加 UITableViewCellAccessoryCheckmark到那一行。
if (indexPath.row == [mutArray indexOfObjectIdenticalTo:[mainViewController.musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyTitle]]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

谈到我的问题,我添加了所有上述(大部分是不相关的)代码,因为我不知道哪里出错了。当我登录 indexOfObjectIdenticalTo:它每次都会吐出“2147483647”,即使数组中的对象永远不会超过 5 个。但为什么?

如果有人有任何提示或指示可以帮助我解决此问题,将不胜感激!

最佳答案

2147483647 只是表示找不到对象。

来自 the documentation of -[NSArray indexOfObjectIdenticalTo:] :

Return Value

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



NSNotFound 定义为:
enum {
   NSNotFound = NSIntegerMax
};

而 2147483647 = 0x7fffffff 是 32 位 iOS 上的最大整数。

请注意,即使两个 NSString 具有相同的内容,它们也可能不是相同的对象。如果两个对象共享相同的位置,则它们是相同的,例如
NSString* a = @"foo";
NSString* b = a;
NSString* c = [a copy];

assert([a isEqual:b]);   // a and b are equal.
assert([a isEqual:c]);   // a and c are equal.
assert(a == b);          // a and b are identical.
assert(a != c);          // a and c are *not* identical.

我相信你只想要平等测试而不是身份测试,即
if (indexPath.row == [mutArray indexOfObject:[....]]) {

关于objective-c - indexOfObjectIdenticalTo : returns value outside of array count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11468893/

相关文章:

objective-c - 什么是引用 socket ?

objective-c - UItableview 不刷新核心数据的更新数据

iphone - Google 路线导航 API (iPhone)

iphone - iOS 在 UITableViewCell 中创建类似 iBook 的界面

ios - Swift 如何从 UITableViewRowAction 获取访问功能

ios - UITableView 在嵌入容器内部的 Controller 之间传递数据

ios - 当用户滚动但 tableview 单元格加载缓慢时,在 tableview 底部显示微调器的好方法是什么?

ios - AVAudioPlayer 实例的过度释放

objective-c - 当 self.edgesForExtendedLayout = UIRectEdgeNone 时,导航栏不再半透明

android - 滚动到 RichText 内的 TextSpan