1、集合:集合(NSSet)和数组(NSArray)有相似之处,都是存储不同的对象的地址
NSSet:可以保证加进来以后的对象唯一,通过hash查询快速,但是无序,杂乱不能确定特定加入之后对象的位置。
NSArray:可以保证装载对象的位置,objectAtIndex方法来取出该位置的对象。还有一些排序的方法,过滤出一个新的数组,但是查找不方便,而且有重复的对象。
2、存储的所有对象只能有唯一一个,不能重复
//初始化
NSSet *set = [[NSSet alloc]initWithObjects:@"a",@"b",@"c",@"one",@"two", nil];
NSSet *set1 = [NSSet setWithObjects:@"three",@"two",@"one",@"a",@"b",@"c", nil];
//数组转换集合 - 会按照数组的顺序排序
NSArray *arr = [NSArray arrayWithObjects:@"3",@"2",@"1", nil];
NSSet *set2 = [NSSet setWithArray:arr];
//输出
NSLog(@"set - %@",set);
NSLog(@"set1 - %@",set1);
NSLog(@"set2 - %@",set2);
//获得集合个数
NSLog(@"set_num -%lu",set.count);
NSLog(@"set1_num - %lu",[set1 count]);
//获取集合中所有的对象 - 返回nsarry
NSArray *arr1 = [set allObjects];
NSLog(@"arr1 - %@",arr1);
//集合中是否包含一个元素
NSLog(@"set中是否包含a %d",[set containsObject:@"a"]);
//集合中是否包含指定set中的对象
NSLog(@"是否包含制定set中的对象 %d",[set1 intersectsSet:set]);
//判断两个SET是否相等
NSLog(@"set是否匹配set1 %d", [set isEqualToSet:set1]);
//是否为子集合
NSLog(@"set是否为set1的子集和 - %d", [set isSubsetOfSet:set1]);
//NSSet添加一个NSArry对象
NSSet *set3 = [NSSet setWithObjects:@"1",@"2",@"3", nil];
NSArray *arr2 = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
NSSet *set4 = [set3 setByAddingObjectsFromArray:arr2];
NSLog(@"set4 - %@",set4);
//添加一个Set - [set3 setByAddingObjectsFromSet:set]
//添加一个id类型的 - [set3 setByAddingObject:<#(nonnull id)#>]
//NSMutableSet
//初始化
NSMutableSet *set5 = [[NSMutableSet alloc]init];
[set5 addObject:@"a"];
NSLog(@"set5 - %@",set5);
//初始化一个大小为size 的set
NSMutableSet *set6 = [[NSMutableSet alloc]initWithCapacity:3];
[set6 addObject:@"a"];
[set6 addObject:@"1"];
[set6 addObject:@"b"];
NSMutableSet *set7 = [NSMutableSet setWithCapacity:3];
[set7 addObject:@"2"];
[set7 addObject:@"3"];
[set7 addObject:@"c"];
//删除指定元素
[set6 removeObject:@"a"];
//集合元素相减
// - (void)minusSet:(NSSet<ObjectType> *)otherSet;
//交集 留下两个集合相同的元素
// - (void)intersectSet:(NSSet<ObjectType> *)otherSet;
//删除所有元素
// - (void)removeAllObjects;
//合集 集合两个集合元素
// - (void)unionSet:(NSSet<ObjectType> *)otherSet;
//清空自身接收新的set所有对象
// - (void)setSet:(NSSet<ObjectType> *)otherSet;
NSMutableSet *ss = [NSMutableSet setWithObjects:@"a",@"b",@"c", nil];
NSMutableSet *ee = [NSMutableSet setWithObjects:@"1",@"2",@"3", nil];
[ss setSet:ee];
NSLog(@"ss - %@",ss);