循环遍历字典数组以查找值

时间:2012-10-31 03:38:47

标签: objective-c ios nsstring nsarray nsdictionary

我有一个字典数组,我想通过它找到匹配的值,然后我可以计算==数组的第N项

数组的每个项目都是这样的

HMOD = 0;
MID = 39;
MOD = SOMETHING; // looking for this value
ID = 50;

所以我想创建一个遍历数组的循环,直到找到匹配的值,然后我使用count中的数字作为下一个视图中Index索引的引用..

我已经编写了这个代码的代码,但是希望它可以让你了解我想要创建的循环。

int count = 0;
while (singleName != [[ModArray valueForKey:@"MOD"] objectAtIndex:count]) {
                    count ++;
                    NSLog(@"%i", count);
                }

SingleName是一个NSString,用于匹配ModArray中的MOD值... 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:10)

通过在字典数组上使用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";

array中获取modArray,其中包含密钥为"MOD"的对象,

    NSArray *array = [modArray valueForKey:@"MOD"];

检查此singleName中是否存在array。如果是,则获取该对象的第一个索引,该索引与MOD中密钥为“modArray”的字典索引相同。

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

答案 1 :(得分:3)

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

答案 2 :(得分:2)

你的代码看起来很全面。你声明你有一系列字典。假设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:的误解,唯一真正的问题是你使用!=来比较两个字符串。