manuelBetancurt
manuelBetancurt

Reputation: 16158

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!

Upvotes: 6

Views: 3398

Answers (1)

EmptyStack
EmptyStack

Reputation: 51374

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.

Upvotes: 17

Related Questions