在数组中查找数字

cou*_*011 0 iphone objective-c

我有一个数组{-1,0,1,2,3,4 ...}我试图找出这些数字中是否存在元素,代码不起作用

NSInteger ind = [favArray indexOfObject:[NSNumber numberWithInt:3]];
Run Code Online (Sandbox Code Playgroud)

在ind我总是得到2147483647

我像这样填充我的阵列

//Loading favArray from favs.plist
    NSString* favPlistPath = [[NSBundle mainBundle] pathForResource:@"favs" ofType:@"plist"];
    NSMutableDictionary* favPlistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:favPlistPath];

    NSString *favString = [favPlistDict objectForKey:@"list"];
    NSArray *favList = [favString componentsSeparatedByString:@","];
    //int n = [[favList objectAtIndex:0] intValue];

    favArray = [[NSMutableArray alloc] initWithCapacity:100];
    if([favList count]>1)
    {
        for(int i=1; i<[favList count]; i++)
        {
            NSNumber *f = [favList objectAtIndex:i];
            [favArray insertObject:f atIndex:(i-1)];
        }
    }
Run Code Online (Sandbox Code Playgroud)

Nik*_*uhe 5

这是NSNotFound的价值,这意味着favArray不包含任何对象isEqual:[NSNumber numberWithInt:3].检查你的阵列.

第二次编辑后:

你的favList数组充满了NSString对象.您应该在将字符串对象NSNumber插入对象之前将其转换为对象favArray:

NSNumber *f = [NSNumber numberWithInt:[[favList objectAtIndex:i] intValue]];
Run Code Online (Sandbox Code Playgroud)