从NSArray获取对象的索引? [英] Getting Index of an Object from NSArray?
问题描述
我试图通过 indexOfObject 方法获取数组的索引,但是当我尝试记录该值以测试索引时,我得到一个垃圾值..出于测试目的,我有一个值为 {57,56,58 ..}
的数组,以获取索引,例如 56
,
i am trying to get index of an array through indexOfObject method as follows but when i try to log the value to test the index i get a garbage value.. for testing purposes i am having an array with values {57,56,58..}
to get an index of lets say 56
,
NSNumber *num = [NSNumber numberWithInteger:56];
NSInteger Aindex = [myArray indexOfObject:num];
NSLog(@" %d",Aindex);
我得到的价值类似于 2323421
。我可能做错了什么?
the value i get is something like 2323421
. what am i possibly doing wrong??
推荐答案
indexOfObject返回的索引
将是您的对象出现的第一个索引。使用 isEqual
方法测试平等。
您获得的垃圾值可能等于 NSNotFound
。$
尝试对它进行 anIndex
测试。您要查找的数字可能不在您的数组中:
The index returned by indexOfObject
will be the first index for an occurence of your object. Equality is tested using isEqual
method.
The garbage value you get is probably equal to NSNotFound
.
Try testing anIndex
against it. The number you are looking for isn't probably in your array :
NSNumber *num=[NSNumber numberWithInteger:56];
NSInteger anIndex=[myArray indexOfObject:num];
if(NSNotFound == anIndex) {
NSLog(@"not found");
}
或记录数组的内容以确定:
or log the content of the array to be sure :
NSLog(@"%@", myArray);
这篇关于从NSArray获取对象的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!