Search NSArray of NSDictionaries

依然范特西╮ 提交于 2019-12-04 20:46:55

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/8340694/search-nsarray-of-nsdictionaries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!