NSArray上一个对象差异的索引 [英] NSArray index of last object discrepancy

查看:65
本文介绍了NSArray上一个对象差异的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个示例,其中包含两个数组,这些数组为其lastObject返回不同的索引.

Below is an example containing two arrays that return different indices for their lastObject.

NSMutableArray *ary0 = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:[NSDecimalNumber decimalNumberWithString:@"2"],[NSDecimalNumber one], nil]];
NSLog(@"%d",[ary0 indexOfObject:[ary0 lastObject]]);

按预期记录1.

NSMutableArray *ary1 = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:[NSDecimalNumber one],[NSDecimalNumber one], nil]];
NSLog(@"%d",[ary1 indexOfObject:[ary1 lastObject]]);

记录0.

即使ary1中有两个相同的对象,我也看不到ary1中lastObject的索引如何为0.有人可以解释为什么这很有意义或指出我正在犯的非常愚蠢的错误吗?谢谢!

I do not see how the index of the lastObject in ary1 is 0, even if there are two identical objects in ary1. Can someone please explain why this makes sense or point out the very silly mistake I'm making? Thanks!

推荐答案

您没有得到最后一个对象的索引,而是得到了与最后一个对象相等的第一个对象的索引. indexOfObject:记录为从数组的第一个元素到最后一个元素的搜索,找到匹配项后停止.

You're not getting the index of the last object, you're getting the index of the first object that is equal to the last object. indexOfObject: is documented as searching from the first element of the array to the last, stopping when it finds a match.

这篇关于NSArray上一个对象差异的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆