ios - extracting properties from NSArray of NSArray of NSDictionary -


this question similar extracting properties nsarray of objects, deeper extraction.

for sake of simplicity, objects referring in below examples nsstrings.

i have 2 cases want solve.

nsarray of nsarray of nsdictionary objects

preferably key-value-coding, following structure, i'd extract "title" single enumerable list:

nsarray *list1 = @[     @[         @{@"title":@"a", @"description":...},         @{@"title":@"b", @"description":...},     ],     @[         @{@"title":@"c", @"description":...},         @{@"title":@"d", @"description":...},     ], ]; 

desired result example:

@[@"a", @"b", @"c", @"d"] 

nsarray of nsdictionary nsarray of objects

preferably key-value-coding, following structure, i'd extract lists of "titles" single enumerable list:

nsarray *list2 = @[     @{         @"titles":@[             @"a",             @"b",         ],         @"descriptions":...,     },     @{         @"titles":@[             @"c",             @"d",         ],         @"descriptions":...,     }, ]; 

desired result example:

@[@"a", @"b", @"c", @"d"] 

notes

  • my real cases involve nsorderedset of nsorderedset of objects first list , nsorderedset of nsdictionary nsorderedset of objects second list, shouldn't affect answers think.

  • i wrote prefer use key-value-coding, not must. want avoid, if possible, writing for (... in ...) or enumateobjectwithblock:.

  • again shouldn't matter, objects nsmanagedobject fetch requests. know optimise fetch requests directly, still want know if there nice alternatives.

kvc can indeed bidding in case, via collection operator called @unionofarrays. 1 effect of operator flatten arrays, first example simple.

[list1 valueforkeypath:@"@unionofarrays.title"] 

the second quite similar, have in reverse order. first extract titles arrays, flatten them.

[list2 valueforkeypath:@"titles.@unionofarrays.self"] 

the self necessary -- although seems redundant -- because, per doc linked above

all collection operators, exception of @count, require key path right of collection operator.

for nsorderedset, seem use array property in key path convert inner collections before operating on them, reason produces errors. found interesting tidbit, however, posted on github nicolas bouilleaud:

// convert each orderedset array mute error. nslog(@"union : %@",[data valueforkeypath:@"@distinctunionofarrays.values.@array"]); 

and weird @array operator works on nsorderedset sample input:

nsorderedset *list1 = [nsorderedset orderedsetwithobjects:[nsorderedset orderedsetwitharray:@[ @{@"title":@"a"}, @{@"title":@"b"} ]], [nsorderedset orderedsetwitharray:@[ @{@"title":@"c"}, @{@"title":@"d"} ]], nil];  // note converting outer set array first     nslog(@"%@", [list1.array valueforkeypath:@"@unionofarrays.title.@array"]);  nsorderedset *list2 = [nsorderedset orderedsetwitharray:@[ @{ @"titles":[nsorderedset orderedsetwithobjects: @"a", @"b", nil] }, @{ @"titles":[nsorderedset orderedsetwithobjects: @"c", @"d", nil] } ]];  // note converting outer set array first nslog(@"%@", [list2.array valueforkeypath:@"titles.@unionofarrays.self.@array"]); 

but i have no idea why. can't figure out comes or it's doing (why it's @ end, in particular).


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -