OC中集合相关操作
//创建一个集合
NSSet *set1 = [NSSet setWithObjects:@"1",@"2",@"3", nil];
NSSet *set2 = [[NSSet alloc] initWithObjects:@"a",@"b",@"c", nil];
NSLog(@"%@",set1);
NSLog(@"%@",set2);
//用数组创建一个集合,集合里的对象唯一,且无序
NSArray *arr1 = [NSArray arrayWithObjects:@"1",@"2",@"3",@"1", nil];
NSSet *set3 = [NSSet setWithArray:arr1];
NSLog(@"%@",set3);
//用已有的集合创建新的集合
NSSet *set4 = [NSSet setWithSet:set2];
NSLog(@"%@",set4);
//以数组的的形式返回集合对象
NSArray *arr2 = [set4 allObjects];
NSLog(@"set4 = %@",arr2);
//返回集合中任意一个对象
id obj = [set3 anyObject];
NSLog(@"%@",obj);
//检查集合中是否含有某个对象,如果有则返回1,否则返回0
BOOL result1 = [set4 containsObject:@"a"];
NSLog(@"%hhd",result1);
//检测2个集合中是否有相同的元素,有相同的返回1,没有返回0
//intersect相交
BOOL result2 = [set4 intersectsSet:set2];
NSLog(@"%hhd",result2);
//检查2个集合内容是否相同
BOOL result3 = [set4 isEqualToSet:set2];
NSLog(@"%hhd",result3);
//检测一个集合是否是另一个的子集
BOOL result4 = [set2 isSubsetOfSet:set4];
NSLog(@"%hhd",result4);
//在一个集合末尾添加一个对象
NSSet *set5 = [set4 setByAddingObject:@"d"];
NSLog(@"%@",set5);
//在集合中添加一个集合,新的集合自动去除重复的对象
NSSet *set6 = [set5 setByAddingObjectsFromSet:set2];
NSLog(@"set6 = %@",set6);
//在集合中添加一个数组,新的集合自动去除重复的对象
NSSet *set7 = [set5 setByAddingObjectsFromArray:arr1];
NSLog(@"set7 = %@",set7);
//集合1减去结合2,即在集合1种去除集合1,2中都有的元素
//minus减去
NSMutableSet *set8 = [NSMutableSet setWithObjects:@"1",@"2",@"3", nil];
NSMutableSet *set9 = [NSMutableSet setWithObjects:@"1",@"a", nil];
[set8 minusSet:set9];
NSLog(@"%@",set8);
//求两个集合的交集
NSMutableSet *set10 = [NSMutableSet setWithObjects:@"1",@"2",@"3", nil];
[set10 intersectSet:set9];
NSLog(@"set10 = %@",set10);
//求2个集合的并集
NSMutableSet *set11 = [NSMutableSet setWithObjects:@"1",@"2",@"3", nil];
[set11 unionSet:set9];
NSLog(@"set11 = %@",set11);
//将一个集合重置为另一个集合
[set9 setSet:set11];
NSLog(@"set9 = %@",set9);
//将数组里的对象加入到集合中
NSArray *arr4 = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
[set9 addObjectsFromArray:arr4];
NSLog(@"new set9 = %@",set9);
//删除集合中的某个对象
[set9 removeObject:@"a"];
NSLog(@"******%@",set9);
//删除集合中的所有对象,即清空集合
[set9 removeAllObjects];
NSLog(@"set9 = %@",set9);