ios - Updating NSDictionary inside an NSArray -
i have nsarray
of nsdictionaries
. need updated nsarray
of nsdictionaries
value of 1 of dictionary key updated new value.
please see below nsarray
structure. want update value key3
inside structure if key1
matches value (e.g. 2). fastest way of doing this. not want use traditional loop.
[myarray valueforkeypath:@"@unionofobjects.key3"]; <__nscfarray 0xe70c10>( { key1 = 1; key2 = "new location"; key3 = ( data1, data2, data3 ); }, { key1 = 2; key2 = "old location"; key3 = ( data1, data2, data4 ); } )
we can solve using predicates:
nsarray *dictionaryarray; nsnumber *key =@2; nspredicate *predicate =[nspredicate predicatewithformat:@"key1=%@",key]; nsarray *result =[dictionaryarray filteredarrayusingpredicate:predicate];
result array has dictionaries having (key1 = 2)
dictionarys can't edited directly, must nsmutabledictionary edit. assuming nsmutabledictionary instances:
nsmutabledictionary *dic =result[0]; [dic setobject:someobject forkey:@"key3"];
Comments
Post a Comment