【问题标题】:loop through array of dictionaries to find a value遍历字典数组以查找值
【发布时间】:2012-10-31 03:38:47
【问题描述】:

我有一个字典数组,我想通过这些字典找到一个匹配的值,然后我可以 Count == 数组的第 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 值... 任何帮助将不胜感激。

【问题讨论】:

    标签: objective-c ios nsstring nsarray nsdictionary


    【解决方案1】:

    这是一个更简单的解决方案,在字典数组上使用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 中获取array,该对象的键为"MOD"

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

    【讨论】:

      【解决方案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: 的误解,唯一真正的问题是您使用 != 来比较两个字符串。

      【讨论】:

      • 您对#2 和#3 的看法是错误的。根据关于 NSArray 的苹果文档,valueForKey: 返回一个数组,其中包含调用 valueForKey: 对每个数组对象使用键的结果。 developer.apple.com/library/mac/#documentation/Cocoa/Reference/… 但是您对 #1 的看法是正确的,您的解决方案也有效。
      • @ACB - 谢谢。经过 4 年多的时间,我从来不知道 NSArray valueForKey: 方法。我希望我有。 :) 我会更新我的答案。
      • :) 有人曾经说过,学习新东西永远不会太晚.. :)
      • @ACB 我承认我什么都不知道 :) API 非常庞大。寻找这样的小宝石是乐趣的一部分。
      • 我完全同意你提到的第二和第三个事实。在阅读所有这些 SO 问题和答案时,我仍然每天都在学习新事物。 :)
      【解决方案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];
          }
      

      【讨论】:

        猜你喜欢
        • 2014-06-18
        • 1970-01-01
        • 2014-11-29
        • 2018-08-22
        • 1970-01-01
        • 2018-09-18
        • 2012-06-02
        • 1970-01-01
        • 2015-08-07
        相关资源
        最近更新 更多