Getting Index of an Object from NSArray?

后端 未结 7 1501
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 23:17

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 t

7条回答
  •  甜味超标
    2020-12-13 00:07

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

提交回复
热议问题