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

4

我有一个字典数组,我想遍历它以找到匹配的值,然后计数==数组的第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是我用来匹配ModArray中MOD值的NSString...任何帮助将不胜感激。
3个回答

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";
modArray 中获取具有键名为 "MOD" 的对象的 array
    NSArray *array = [modArray valueForKey:@"MOD"];

检查singleName是否存在于此array中。如果是,则获取该对象的第一个索引,该索引将与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);
                }

3

你的代码看起来很混乱。你说你有一个字典的数组。假设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:的误解,唯一的问题是你使用!=来比较两个字符串。

1
你对第二点和第三点的理解是错误的。根据苹果关于NSArray的文档,valueForKey返回一个数组,其中包含使用key在数组中的每个对象上调用valueForKey的结果。https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html 然而,你对第一点的理解是正确的,你的解决方案也可行。 - iDev
@ACB - 谢谢。经过4年多的编程,我从未听说过NSArray valueForKey:方法。我真希望早点知道它。 :) 我会更新我的答案。 - rmaddy
有人曾经说过,学习新东西永远不会太晚。 :) - iDev
1
@ACB 我毫不在意承认我不知道所有的东西 :) 这个API非常庞大。找到像这样的小宝石是其中有趣的部分。 - rmaddy
我完全同意你提到的第二和第三个事实。在浏览所有这些SO问题和答案时,我仍然每天学习新东西。 :) - iDev

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接