簡體   English   中英

許多 NSDictionary 的 NSArray。 為給定鍵找到具有必要值的 NSDictionary 的最佳方法是什么?

[英]NSArray of many NSDictionary. What is the best way to find a NSDictionary with necessary value for given key?

現在我正在嘗試以下方法並且它有效。


    - (void)findDictionaryWithValueForKey:(NSString *)name {

         for (NSDictionary * set in myArray) {

            if ([[set objectForKey:@"title"] isEqualToString:name]) 
               \\do something

         }

    }

編輯:我在bshirley的帖子中添加了一個額外的論點。 現在它看起來更靈活。



- (NSDictionary *)findDictionaryWithValue:(NSString*)name forKey:(NSString *)key {

    __block BOOL found = NO;
    __block NSDictionary *dict = nil;

    [self.cardSetsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        dict = (NSDictionary *)obj;
        NSString *title = [dict valueForKey:key];
        if ([title isEqualToString:name]) {
            found = YES;
            *stop = YES;
        }
    }];

    if (found) {
        return dict;
    } else {
        return nil;
    }

}

使用filteredArrayUsingPredicate:數組的方法來獲取滿足您要求的所有字典。

NSPredicate * predicate = [NSPredicate predicateWithFormat:@" title MATCHES[cd] %@", name];
NSArray * matches = [myArray filteredArrayUsingPredicate:predicate];

現在matchestitle鍵等於name的字典數組。

這是使用較新的 API 的一種可能實現。 (我還修改了方法以實際返回值)。 主要用於演示 API。 假設標題對於數組中的一個字典是唯一的。

- (NSDictionary *)findDictionaryWithValueForKey:(NSString *)name {
  // ivar: NSArray *myArray;
  __block BOOL found = NO;
  __block NSDictionary *dict = nil;

  [myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    dict = (NSDictionary *)obj;
    NSString *title = [dict valueForKey:@"title"];
    if ([title isEqualToString:name]) {
      found = YES;
      *stop = YES;
    }
  }];

  if (found) {
    return dict;
  } else {
    return nil;
  }

}

我也會建議這種方式,如果你使用break語句會更好,

- (void)findDictionaryWithValueForKey:(NSString)name {

     for (NSDictionary * set in myArray) {

        if ([[set objectForKey:@"title"] isEqualToString:name]) 
           \\do something
           break;
     }

}

根據 NSArray 文檔,

valueForKey:

Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
- (id)valueForKey:(NSString *)key
Parameters

key

    The key to retrieve.

Return Value

The value of the retrieved key.
Discussion

The returned array contains NSNull elements for each object that returns nil.
Availability

    * Available in Mac OS X v10.3 and later.

編輯:

嘗試這個,

[myArray valueForKey:@"name"];

//這將返回值數組,但這實際上與想要的不同

    - (void)findDictionaryWithValueForKey:(NSString)name {

        for (NSDictionary * set in myArray) {


            NSString *s=[NSString stringWithFormat:@"%@",[set objectForKey:@"title"]];
            if ([s isEqualToString:name]) 
                \\do something

            }

            OR 
            if (s == name]) 
                \\do something

            }



    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM