我如何在具有字典的数组中找到 indexOfObject
How do i find indexOfObject in an array that has dictionaries
这是我的数组:
<__NSCFArray 0x7b6ca390>(
{
name = kumar;
no = 158;
},
{
name = rajdeep;
no = 338;
},
{
name = smitha;
no = 361;
},
{
name = uma;
no = 422;
}
)
这就是我想要做的
NSInteger selectedIndexpath=[self.arrLoadCell indexOfObject:_txtfield.text];
其中
_txtfield.text = @"rajdeep"
我得到一些随机垃圾值,如 2147483647 存储在 selectedIndexPath 中。
我错过了什么吗?或者有什么其他方法可以处理吗?
这个"junk value"好像是NSNotFound
,你在使用这个方法的时候应该对照一下。长话短说,您的数组不包含您要查找的值。它不起作用,因为您正在寻找一个字符串,但该数组包含字典,因此您也必须搜索例如{ "name" : "rajdeep", "no" : @338 }
.
或者,要使其仅适用于字符串,请使用 NSPredicate
进行过滤,例如
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", valueFromTextbox];
NSArray result = [array filteredArrayUsingPredicate: predicate];
如果你的数组很短,你也可以做一个循环来比较。
Update获取索引,使用谓词的结果作为输入,例如
NSInteger selectedIndexpath = [self.arrLoadCell indexOfObject:result.firstObject];
那是因为你的数组包含字典,而不是字符串。您不能将字符串 (textfield.text) 与数组中的字典进行比较。
indexOfObject:需要存储在数组中的实际对象。您仅提供其中一个词典的键值。
实现所需目标的方法之一是遍历数组并针对每个字典测试值。
你得到的"junk value"是NSNotFound,表示没有找到对象
我想你想要的是:
int idx = NSNotFound;
for (int i=0; i<self.arrLoadCell.count; i++) {
NSDictionary *dict = [self.arrLoadCell objectAtIndex:i];
if ([[dict objectForKey:@"name"] isEqualToString:_txtfield.text]) {
idx = i;
break;
}
}
NSInteger selectedIndexpath=idx;
NSUInteger indexPath = 0;
for (NSDictionary *dic in arrLoadCell){
if ([[dic objectForKey:@"name"] isEqualToString: _txtfield.text]) {
indexPath=[arrLoadCell indexOfObject:dic];
break;
}
}
NSLog(@"%lu is the indexPATH",(unsigned long)indexPath);
假设您的词典数组是 arrLoadCell,这应该可行。
这是我的数组:
<__NSCFArray 0x7b6ca390>(
{
name = kumar;
no = 158;
},
{
name = rajdeep;
no = 338;
},
{
name = smitha;
no = 361;
},
{
name = uma;
no = 422;
}
)
这就是我想要做的
NSInteger selectedIndexpath=[self.arrLoadCell indexOfObject:_txtfield.text];
其中
_txtfield.text = @"rajdeep"
我得到一些随机垃圾值,如 2147483647 存储在 selectedIndexPath 中。 我错过了什么吗?或者有什么其他方法可以处理吗?
这个"junk value"好像是NSNotFound
,你在使用这个方法的时候应该对照一下。长话短说,您的数组不包含您要查找的值。它不起作用,因为您正在寻找一个字符串,但该数组包含字典,因此您也必须搜索例如{ "name" : "rajdeep", "no" : @338 }
.
或者,要使其仅适用于字符串,请使用 NSPredicate
进行过滤,例如
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", valueFromTextbox];
NSArray result = [array filteredArrayUsingPredicate: predicate];
如果你的数组很短,你也可以做一个循环来比较。
Update获取索引,使用谓词的结果作为输入,例如
NSInteger selectedIndexpath = [self.arrLoadCell indexOfObject:result.firstObject];
那是因为你的数组包含字典,而不是字符串。您不能将字符串 (textfield.text) 与数组中的字典进行比较。
indexOfObject:需要存储在数组中的实际对象。您仅提供其中一个词典的键值。
实现所需目标的方法之一是遍历数组并针对每个字典测试值。
你得到的"junk value"是NSNotFound,表示没有找到对象
我想你想要的是:
int idx = NSNotFound;
for (int i=0; i<self.arrLoadCell.count; i++) {
NSDictionary *dict = [self.arrLoadCell objectAtIndex:i];
if ([[dict objectForKey:@"name"] isEqualToString:_txtfield.text]) {
idx = i;
break;
}
}
NSInteger selectedIndexpath=idx;
NSUInteger indexPath = 0;
for (NSDictionary *dic in arrLoadCell){
if ([[dic objectForKey:@"name"] isEqualToString: _txtfield.text]) {
indexPath=[arrLoadCell indexOfObject:dic];
break;
}
}
NSLog(@"%lu is the indexPATH",(unsigned long)indexPath);
假设您的词典数组是 arrLoadCell,这应该可行。