开发者

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

开发者 https://www.devze.com 2023-03-19 01:22 出处:网络
Now I\'m trying the following and it works. - (void)findDictionaryWithValueForKey:(NSString *)name { for (NSDictionary * set in myArray) {

Now I'm trying the following and it works.


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

         for (NSDictionary * set in myArray) {

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

         }

    }

EDIT: I've added one extra argument to the post of bshirley. Now it looks more flexible.



- (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 {
开发者_Go百科        return nil;
    }

}


Here's one possible implementation using newer API. (I also modified the method to actually return the value). Provided mostly to demonstrate that API. The assumption is that the title is unique to one dictionary within your array.

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

}


Use filteredArrayUsingPredicate: method of the array to get all the dictionaries that satisfy your requirement.

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

Now matches is the array of dictionaries that have the title key equal to 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

            }



    }


I will also suggest this way,it would be better if you use a break statement,

- (void)findDictionaryWithValueForKey:(NSString)name {

     for (NSDictionary * set in myArray) {

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

}

As per the NSArray documentation,

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.

EDIT:

try this,

[myArray valueForKey:@"name"];

//this will return array of values, but this actually differ from what to want

0

精彩评论

暂无评论...
验证码 换一张
取 消