我有一个字典数组,我想要遍历它们以找到一个匹配值,然后我可以计算该数组的第N个==项
该数组的每一项看起来如下所示
HMOD = 0;
MID = 39;
MOD = SOMETHING; // looking for this value
ID = 50;因此,我想创建一个循环,该循环遍历数组,直到找到匹配值,然后使用计数中的数字作为对下一个视图中索引路径的引用。
我写了这段不能工作的代码...但希望它能让你对我试图创建的循环有一个概念。
int count = 0;
while (singleName != [[ModArray valueForKey:@"MOD"] objectAtIndex:count]) {
count ++;
NSLog(@"%i", count);
}SingleName是一个NSString,我用它来匹配ModArray中的MOD值...任何帮助都将不胜感激。
发布于 2012-10-31 13:07:44
这里有一个更简单的解决方案,通过在字典数组上使用valueForKey,
假设您的modArray是这样的,
NSArray *modArray = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:@"0" forKey:@"HMOD"],
[NSDictionary dictionaryWithObject:@"39" forKey:@"MID"],
[NSDictionary dictionaryWithObject:@"something" forKey:@"MOD"],
[NSDictionary dictionaryWithObject:@"50" forKey:@"ID"], nil];singleName的值为"something"
NSString *singleName = @"something";从具有关键字为"MOD"的对象的modArray中获取array,
NSArray *array = [modArray valueForKey:@"MOD"];检查此array中是否存在singleName。如果是,则获取该对象的第一个索引,该索引将与modArray中关键字为"MOD“的字典的索引相同。
if ([array containsObject:singleName]) {
NSLog(@"%d", [array indexOfObject:singleName]);
} else {
NSLog(@"%@ is not present in the array", singleName);
}更新:如果你想用你自己的方式去做,唯一的错误就是你使用的是!=,而你应该使用isEqualToString。你应该这样做的,
int count = 0;
while (![singleName isEqualToString:[[modArray valueForKey:@"MOD"] objectAtIndex:count]]) {
count ++;
NSLog(@"%i", count);
}发布于 2015-12-17 19:55:21
- This is Helpful when you search from Dictionary.
NSMutableArray *contentList;
NSMutableArray *filteredContentList;
BOOL isSearching;
// firstSection is array which already filled.
// contentList array for value of particular key
// filteredContentList is search array from actual array.
- (void)searchTableList {
NSString *searchString = searchBar.text;
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"frame_code beginswith[c] %@", searchString];
NSArray *filteredArr = [firstSection filteredArrayUsingPredicate:filterPredicate];
if(contentList.count > 0)
[contentList removeAllObjects];
[filteredContentList addObjectsFromArray:filteredArr];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar1 {
if ([searchBar1.text length] != 0)
isSearching = YES;
else
isSearching = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"Text change - %d",isSearching);
//Remove all objects first.
[filteredContentList removeAllObjects];
if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];
}
else {
isSearching = NO;
}
[tblFrameList_SComplete reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
[self searchTableList];
}发布于 2012-10-31 11:56:16
你的代码看起来都是从里到外的。你声明你有一个字典数组。假设ModArray是数组(基于名称),您可以这样做:
NSUInteger count = 0;
for (NSDictionary *dict in ModArray) { // iterate through the array
NSString *mod = dict[@"MOD"]; // get the value for MOD
if ([mod isEqualToString:singleName]) { // compare the two strings
break; // they match so exit the loop
}
count++
}
// count has the index of the dictionary with the matching MOD value编辑:基于ACB纠正了我对NSArray valueForKey:的误解,唯一真正的问题是您使用!=来比较这两个字符串。
https://stackoverflow.com/questions/13150924
复制相似问题