Sunday 15 July 2012

ios - Searching on an array of array of dictionaries by a key -



ios - Searching on an array of array of dictionaries by a key -

i have dictionaries this

nsdictionary *dicta = @{ @"name" : @"john", @"city" : @"los angeles" }; nsdictionary *dictb = @{ @"name" : @"bob", @"city" : @"los angeles" }; nsdictionary *dictc = @{ @"name" : @"carl", @"city" : @"dallas" };

dozens of dictionaries separated city , set within other dictionaries like

nsarray *losangeles = @[ dicta, dictb, ....]; nsarray *dallas = @[ dictc, ... ];

and within array

self.arrayall = @[losangeles, dallas, miami... ];

now improve part, want dictionary contains person named bob (suppose names unique).

if array of dicts this

nspredicate *pred = [nspredicate predicatewithformat:@"(name beginswith[cd] %@)", @"bob"]; nsarray *filterednames = [self.arrayall filteredarrayusingpredicate:pred];

but because arrayall array of array of dicts, not working next error:

*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'can't substring operation isn't string ????

any clues?

i'd this...

nsmutablearray *results = [nsmutablearray array]; [citiesarray enumerateobjectsusingblock:^(nsarray *dictarray, nsuinteger idx, bool *stop) { nspredicate *pred = [nspredicate predicatewithformat:@"(name beginswith[cd] %@)", @"bob"]; nsarray *filterednames = [self.arrayall filteredarrayusingpredicate:pred]; [results addobjectsfromarray:filterednames]; }];

you perchance add together concurrency alternative this...

[citiesarray enumerateobjectswithoptions:nsenumerationconcurrent usingblock:^(nsarray *dictarray, nsuinteger idx, bool *stop) { //stuff... }];

to have run concurrently too. don't know how results array handle though. give try.

this exploit fast enumeration , concurrency steps.

ios cocoa-touch nsarray nsdictionary nspredicate

No comments:

Post a Comment