与Array的区别
在内存中存储的方式是不连续的
在搜索一个个元素的时候效率更高 因为使用的是hash 比如说找一个元素 set通过hash算法可以直接找到 而Array就需要通过遍历
可以求交集并集
NSSet
初始化
NSSet*set = [[NSSet alloc]initWithObjects:@"a",@"b",nil];
NSSet*set2 = [set2 setWithArray]
查
if([set containsObject:@"a"])NSLog(@"YES");
求交集
//set(a,b,c). set2(a,s,d,)
if([set intersectsSet:set2])NSLog(@"YES");//求是否有相同元素
[set intersectSet:set2];
NSLog(@"%@",set);//a
求并集
//set(a,b,c). set2(a,s,d,)
[set unionSet:set2];//会将set2和se1中共同的都加到set中 会去重
NSLog(@“%@”,set);//a,b,c,s,d
求补集
//set(a,b,c). set2(a,s,d,)
[set minusSet:set2];
NSLog(@“%@”,set);//b,c
NSMutableSet
NSMutableSet*mset = [[NSMutableSet alloc]initWithCapacity:0];
[set addObject:@"a"];