ios 从 NSArray 获取对象的索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7398141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:29:45  来源:igfitidea点击:

Getting Index of an Object from NSArray?

iphoneiosnsarray

提问by hemant

i am trying to get index of an array through indexOfObjectmethod 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,

我正在尝试通过indexOfObject方法获取数组的索引,如下所示,但是当我尝试记录值以测试索引时,我得到一个垃圾值..出于测试目的,我有一个包含值的数组{57,56,58..}来获取索引56,

NSNumber *num = [NSNumber numberWithInteger:56];
NSInteger Aindex = [myArray indexOfObject:num];
NSLog(@" %d",Aindex);

the value i get is something like 2323421. what am i possibly doing wrong??

我得到的价值是这样的2323421。我可能做错了什么?

回答by

The index returned by indexOfObjectwill be the first index for an occurence of your object. Equality is tested using isEqualmethod.
The garbage value you get is probably equal to NSNotFound.
Try testing anIndexagainst it. The number you are looking for isn't probably in your array :

返回的索引indexOfObject将是对象出现的第一个索引。使用isEqual方法来测试相等性。
你得到的垃圾值可能等于NSNotFound.
尝试对其进行测试anIndex。您要查找的数字可能不在您的数组中:

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);

回答by joe

Folks,

各位,

When an object is not found in the array the indexOfObject method does NOT return a 'garbage' value. Many systems return an index of -1 if the item is not found.

当在数组中找不到对象时,indexOfObject 方法不会返回“垃圾”值。如果未找到该项目,许多系统将返回 -1 的索引。

However, on IOS - because the indexOfObject returns an UNSIGNED int (aka NSUInteger) the returned index must be greater than or equal to zero. Since 'zero' is a valid index there is no way to indicate to the caller that the object was not found -- except by returning an agreed upon constant value that we all can test upon. This constant agreed upon value is called NSNotFound.

但是,在 IOS 上 - 因为 indexOfObject 返回一个 UNSIGNED int(又名 NSUInteger),所以返回的索引必须大于或等于零。由于“零”是一个有效的索引,因此无法向调用者表明未找到该对象——除非返回一个我们都可以测试的商定的常量值。这个常量商定的值称为 NSNotFound。

The method:

方法:

- (NSUInteger)indexOfObject:(id)anObject;

will return NSNotFound if the object was not in the array. NSNotFound is a very large POSITIVE integer (usually 1 minus the maximum int on the platform).

如果对象不在数组中,将返回 NSNotFound。NSNotFound 是一个非常大的正整数(通常是 1 减去平台上的最大整数)。

回答by iCoder4777

NSNumber *num1 = [NSNumber numberWithInt:56];
    NSNumber *num2 = [NSNumber numberWithInt:57];
    NSNumber *num3 = [NSNumber numberWithInt:58];
    NSMutableArray *myArray = [NSMutableArray arrayWithObjects:num1,num2,num3,nil];
    NSNumber *num=[NSNumber numberWithInteger:58];

    NSInteger Aindex=[myArray indexOfObject:num];

    NSLog(@" %d",Aindex);

Its giving the correct output, may be u have done something wrong with storing objects in ur array.

它给出了正确的输出,可能是你在你的数组中存储对象时做错了什么。

回答by Rajesh Loganathan

Try this:

尝试这个:

NSArray's indexOfObject:method. Such as the following:

NSArray 的indexOfObject:方法。例如以下内容:

NSUInteger fooIndex = [someArray indexOfObject: someObject];

回答by Josh O'Connor

If you're using Swift and optionals make sure they are unwrapped. You cannot search the index of objects that are optionals.

如果您使用 Swift 和可选项,请确保它们已解包。您无法搜索可选对象的索引。

回答by Jayasabeen

indexOfObject methord will get the index of the corresponding string in that array if the string is like @"Test" and you find like @"TEST" Now this will retun an index like a long number

indexOfObject 方法将获取该数组中相应字符串的索引,如果字符串类似于 @"Test" 并且您发现类似于 @"TEST" 现在这将返回一个像长数一样的索引

回答by visakh7

I just checked. Its working fine for me. Check if your array has the particular number. It will return such garbage values if element is not present.

我刚查过。它对我来说工作正常。检查您的阵列是否具有特定编号。如果元素不存在,它将返回这样的垃圾值。