Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search NSArray of NSDictionaries

I have an array with dictionaries, and need to search trough the array

and modify a specific dictionary in the array found by an object name inside the dictionary.

So , create the mutable array, dictionary , and add many dictionaries to the array

       ...{ self.bloquesArray = [[[NSMutableArray alloc] init]autorelease];  
    [self createBloqueDicto];
    [self.unBloqueDicto setObject:@"easySprite" forKey:@"Name"];
    [self.unBloqueDicto setObject:@"290" forKey:@"X"];
    [self.unBloqueDicto setObject:@"300" forKey:@"Y"];

    [self.bloquesArray addObject:self.unBloqueDicto];

}

- (void)createBloqueDicto {
self.unBloqueDicto = [[[NSMutableDictionary alloc] init] autorelease];
}

so now I need to change the value for the key X and Y in the dictionary with

key: Name = easySprite so need to find that specific dictionary [other dictionaries have different values for Name]

how can I do this please?

thanks!

like image 815
manuelBetancurt Avatar asked Dec 01 '11 11:12

manuelBetancurt


1 Answers

Do the following to get the matched dictionaries,

NSPredicate *p = [NSPredicate predicateWithFormat:@"Name = %@", @"easySprite"];
NSArray *matchedDicts = [bloquesArray filteredArrayUsingPredicate:p];

Now the matchedDicts contains the dictionaries with the value @"easySprite" for the key @"Name". Do the rest(changing the X and Y) from there.

like image 97
EmptyStack Avatar answered Sep 22 '22 09:09

EmptyStack