在数组中查找数字

时间:2010-07-08 12:01:24

标签: objective-c iphone

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

NSInteger ind = [favArray indexOfObject:[NSNumber numberWithInt:3]];

在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)];
        }
    }

1 个答案:

答案 0 :(得分:5)

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

第二次编辑后:

您的favList数组中填充了NSString个对象。在将字符串对象插入NSNumber

之前,应将其转换为favArray个对象
NSNumber *f = [NSNumber numberWithInt:[[favList objectAtIndex:i] intValue]];