How do I merge 2 NSSets in objective-c?
Asked Answered
W

3

14

How do I merge 2 NSSets in objective-c ?

I can't find solution on google.

Willyt answered 12/8, 2011 at 12:16 Comment(0)
A
32

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

- (NSSet *) setByAddingObjectsFromSet:(NSSet*) other;
Aquavit answered 12/8, 2011 at 12:27 Comment(0)
U
5

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];
Unspeakable answered 25/5, 2014 at 9:7 Comment(0)
S
2

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];
Sculpt answered 9/12, 2013 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.