0

I have a NSArray of NSDictionaries. Each NSDictionary contains a key with value as a NSArray of NSString (see below). I do not want to loop through and do the traditional comparison. So I though of using valueForKeyPath but is returns me arrays of data back and not single array of all strings.

Expected output is Key3 = (Data1, Data2, Data3, Data4);

Any idea.

[myArray valueForKeyPath:@"@unionOfObjects.Key3"];

<__NSCFArray 0xe70c10>(
{
    Key1 = 1;
    Key2 = "New Location";
    Key3 =     (
        Data1,
        Data2,
        Data3
    );
},
{
    Key1 = 2;
    Key2 = "Old Location";
    Key3 =     (
        Data1,
        Data2,
        Data4
    );
}
)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Take a look at NSSet for unions, e.g. http://stackoverflow.com/questions/7616470/combining-nsarrays-through-intersection-and-union – Marco Jan 30 '14 at 23:47

1 Answers1

3

You can use an NSMutableSet to do the union. The only downside is that you lose the ordering. If that's important, look at NSMutableOrderedSet, or handle the logic yourself.

NSArray *arrayOfStringsArrays = [myArray valueForKeyPath:@"Key3"];
NSMutableSet *allStrings = [NSMutableSet new];
for (NSArray *anArrayOfStrings in arrayOfStringsArrays)
{
    [allStrings addObjectsFromArray:anArrayOfStrings];
}
NSArray *allStringsAsArray = [allStrings allObjects];
Nick Forge
  • 21,344
  • 7
  • 55
  • 78