我有一个数组{-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)];
}
}
答案 0 :(得分:5)
这是NSNotFound的值,这意味着favArray不包含isEqual:
到[NSNumber numberWithInt:3]
的对象。检查你的阵列。
第二次编辑后:
您的favList
数组中填充了NSString
个对象。在将字符串对象插入NSNumber
:
favArray
个对象
NSNumber *f = [NSNumber numberWithInt:[[favList objectAtIndex:i] intValue]];