开发者

How do I merge 2 NSSets in objective-c?

开发者 https://www.devze.com 2023-03-27 11:34 出处:网络
How do I merge 2 NSSets in objective-c ? I can\'t fin开发者_JAVA技巧d solution on google.This is fairly easy to spot among NSSet\'s methods:

How do I merge 2 NSSets in objective-c ?

I can't fin开发者_JAVA技巧d solution on google.


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

- (NSSet *) setByAddingObjectsFromSet:(NSSet*) other;


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号