Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I merge 2 NSSets in objective-c?

Tags:

cocoa

nsset

How do I merge 2 NSSets in objective-c ?

I can't find solution on google.

like image 253
aneuryzm Avatar asked Aug 12 '11 12:08

aneuryzm


3 Answers

This is fairly easy to spot among NSSet's methods:

- (NSSet *) setByAddingObjectsFromSet:(NSSet*) other;
like image 200
hamstergene Avatar answered Nov 16 '22 07:11

hamstergene


If one of the sets is an NSMutableSet then you can use a union operation, like in the following example:

// Create / Get the sets
NSMutableSet *firstSet = [NSMutableSet setWithArray:@[@"1", @"2"]];
NSSet *secondSet = [NSSet setWithArray:@[@"3",@"4"]];

// Add missing values from the second set to the first set
[firstSet unionSet:secondSet];
like image 28
Tom Susel Avatar answered Nov 16 '22 07:11

Tom Susel


You can use this if you are merging two set.

NSSet *mergedSet = [set setByAddingObjectsFromSet:set];

If you are merging array into set then you can use

NSSet *mergedSet = [set setByAddingObjectsFromArray:array];
like image 2
Nanda Avatar answered Nov 16 '22 07:11

Nanda