How would I combine two arrays in Objective-C?

NSArray‘s arrayByAddingObjectsFromArray: is more-or-less equivalent to JavaScript’s .concat() method:

NSArray *newArray=[firstArray arrayByAddingObjectsFromArray:secondArray];

Note: If firstArray is nil, newArray will be nil. This can be fixed by using the following:

NSArray *newArray=firstArray?[firstArray arrayByAddingObjectsFromArray:secondArray]:[[NSArray alloc] initWithArray:secondArray];

If you want to strip-out duplicates:

NSArray *uniqueEntries = (NSArray *)[[NSSet setWithArray:newArray] allObjects];

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)