Union of objects in NSArray

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
    );
}
)

Answers


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];

Need Your Help

Show/Hide only show current content

jquery html css

Sorry about the title name I didn't know how to explain this lol..

PDO prepared statements in functions

php mysql

I am currently using MySQL with PHP but am looking to start MySQLi or PDO