iphone - Filtering NSDictionary with predicate -
i using nsdictionary contains dictionaries keys , values.the format ,
{ "1" = { "key1" = "ss", "key2" = "rr", "name" = "nm" }, "2" = { "key1" = "tt", "key2" = "vv", "name" = "gf" }, "3" = { "key1" = "nm", "key2" = "vv", "name" = "gf" }, "4" = { "key1" = "tt", "key2" = "vv", "name" = "gf" }, }
i need filter case key1 should "tt" , key2 should "vv" using nspredicate.
assume
maindict = { "1" = { "key1" = "ss", "key2" = "rr", "name" = "nm" }, "2" = { "key1" = "tt", "key2" = "vv", "name" = "gf" }, "3" = { "key1" = "nm", "key2" = "vv", "name" = "gf" }, "4" = { "key1" = "tt", "key2" = "vv", "name" = "gf" }, }
now can filter following way:
nsarray *resultarray = [[maindict allvalues] filteredarrayusingpredicate:[nspredicate predicatewithformat:@"(key1 == %@) , (key2==%@)", @"tt",@"vv"]];
try check:
nsmutabledictionary *maindict=[[nsmutabledictionary alloc] init]; for(int i=1; i<=3; i++) { [maindict setobject:[nsdictionary dictionarywithobjectsandkeys:@"tt",@"key1",@"vv",@"key2",@"ttqwdwd",@"name", nil] forkey:[nsstring stringwithformat:@"%i",i]]; } [maindict setobject:[nsdictionary dictionarywithobjectsandkeys:@"tt",@"key1",@"kk",@"key2",@"ttwwdwd",@"name", nil] forkey:[nsstring stringwithformat:@"%i",4]]; [maindict setobject:[nsdictionary dictionarywithobjectsandkeys:@"tt",@"key1",@"kk",@"key2",@"ttwwdwd",@"name", nil] forkey:[nsstring stringwithformat:@"%i",5]]; nsarray *resultarray = [[maindict allvalues] filteredarrayusingpredicate:[nspredicate predicatewithformat:@"(key1 == %@) , (key2==%@)", @"tt",@"vv"]]; nslog(@"%@",resultarray);
Comments
Post a Comment