1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
//NSSet类是一组单值对象的集合,且NSSet实例中元素是无序的,同一个对象只能保存一个 /*******************不可变集合****************/ //集合的创建 //方式一 NSSet *set1 = [NSSet setWithObject:@ "1" , @ "2" , nil];
//方式二 NSSet *set2 = [[NSSet alloc] initWithObejects:@ "1" , @ "2" , @ "3" , nil];
//方式三 NSArray *array1 = [NSArray arrayWithObjects:@ "4" , @ "5" , nil];
NSSet *set3 = [NSSet setWithArray:array1]; //方式四 NSSet *set4 = [NSSet setWithSet:set1]; //获取集合元素个数 int count = [set2 count];
//获取集合元素 NSArray *objects = [set2 allObjects]; NSLog(@ "%@" , objects); //输出:(1,2,3)
//获取集合中任意一个对象 id object = [set2 anyObject]; //判断集合是否包含某元素 BOOL isContain = [set4 containsObject:@ "1" ]; //输出:isContain=1
//判断集合间是否存在交集 BOOL isIntersect = [set1 intersectSet:set2];
//判断是否匹配(元素都相同) BOOL isEqual = [set1 isEqualToSet:set2];
//判断一个集合是否是另一个集合的子集 BOOL isSub = [set1 isSubsetOfSet:set2];
//追加一个元素 NSSet *set5 = [NSSet setWithObjects:@ "one" ];
NSSet *appSet1 = [set5 setByAddingObject:@ "two" ]; //输出:appSet1={one,two}
//追加一个集合 //方式一 NSSet *appSet2 = [set5 setByAddingObjectsFromSet:set3]; //方式二 NSArray *array2 = [NSArray arrayWithObject:@ "end" ];
NSSet *appSet3 = [set5 setByAddingObjectsFromArray:array2]; /***************可变集合********************/ //创建一个空的集合 NSMutableSet *set1 = [NSMutableSet set]; NSMutableSet *set2 = [NSMutable setWithObjects:@ "1" , @ "2" , nil];
NSMutableSet *set3 = [NSMutable setWithObjects:@ "a" , @ "2" , nil];
//集合2“减去”集合3中的元素,集合2最后元素只有一个,且为1 [set2 minusSet:set3]; //集合2与集合3中元素的交集,集合2最后元素只有一个,且为2 [set2 intersectSet:set3] //集合2与集合3中元素的并集,集合2最后元素有三个,为1,2,a [set2 unionSet:set3]; //将空集合1设置为集合3中的内容 [set1 setSet:set3]; //根据数组中的内容删除集合中的对象 [set2 removeObjectsFromArray:array]; [set2 removeObject:@ "1" ];
[set2 removeAllObjects]; |
NSSet集合
最新推荐文章于 2016-08-18 09:20:00 发布