应用场景
1. 去除重复元素
NSArray *arrayWithDuplicates = @[@1, @2, @2, @3, @3, @3];
NSSet *uniqueSet = [NSSet setWithArray:arrayWithDuplicates];
NSArray *uniqueArray = [uniqueSet allObjects];2. 快速查找元素
NSSet *set = [NSSet setWithObjects:@"apple", @"banana", @"orange", nil];
BOOL containsApple = [set containsObject:@"apple"];3. 集合运算
NSSet *set1 = [NSSet setWithObjects:@"A", @"B", @"C", nil];
NSSet *set2 = [NSSet setWithObjects:@"B", @"C", @"D", nil];
// 并集
NSSet *unionSet = [set1 setByAddingObjectsFromSet:set2];
// 交集
NSSet *intersectionSet = [set1 setByIntersectingSet:set2];
// 差集
NSSet *differenceSet = [set1 setBySubtractingSet:set2];4. 存储不关心顺序的数据
Last updated