*********************** 不可变集合 Immutable Set***********************
1、 可变集合的创建
6、 取交集
1、
创建对象集合
set
- NSSet *set1 = [[NSSet alloc] initWithObjects:str1,str2, nil];
- NSArray *array = [NSArray arrayWithObjects:str1,str2,@"zzxczv", nil];
2、
将数组
放到集合
中
- NSSet *set3 = [NSSet setWithArray:array];
3、
将集合转换为数组
- NSArray *array1 = [set3 allObjects];
4、
集合中元素个数
- NSLog(@"集合中元素个数set3.count = %ld",[set3 count]);
5、
从集合中获取某元素,不能保证随机
- NSString *str3 = [set3 anyObject];
6、
集合中
没有
重复元素
- NSSet *set4 = [NSSet setWithObjects:str1,str1,str2,str2, nil];
7、将
一个集合
赋给
另一个集合
- NSSet *set5 = [[NSSet alloc] initWithSet:set4];
8、
两集合相加
- NSSet *set6 = [set5 setByAddingObjectsFromSet:set3];
- NSLog(@"set5 = %@",set5); //不变
-
1、 可变集合的创建
- NSMutableSet *mSet1 = [[NSMutableSet alloc] initWithCapacity:2];
- NSString *str7 = @"What do you want?";
- [mSet1 addObject:str7];
- NSMutableSet *mSet2 = [NSMutableSet setWithObjects:str1,str2,mSet1, nil];
- [mSet2 removeObject:@"qwertyuiop"];
- [mSet2 unionSet:set1];
6、 取交集
- //Removes from the receiving set each object that isn’t a member of another given set.
- [mSet2 intersectSet:set1];
- [mSet2 removeAllObjects];
- NSMutableSet *mSet3 = [[NSMutableSet alloc] initWithCapacity:3];
- [mSet3 setSet:set1];
- *[mSet3 setSet:mSet2];//在此之前 mSet2 已被清空。该行执行后,mSet3 = {()} 空集合