1 //集合的创建
2
3 //集合内不包含重复元素
4
5 NSSet *set1 = [NSSet setWithObjects:@"1",@"2", nil];
6
7 NSSet *set2 = [[NSSet alloc] initWithObjects:@"3",@"4", nil];
8
9 NSArray *array1 = [NSArray arrayWithObjects:@"5",@"6", nil];
10
11 NSSet *set3 = [NSSet setWithArray:array1];
12
13 NSSet *set4 = [NSSet setWithSet:set1];
14
15
16
17 //集合3种对象的个数
18
19 int count = [set3 count];
20
21
22
23 //以数组形式返回集合3中所有的对象
24
25 NSArray *allObjects = [set3 allObjects];
26
27
28
29 //返回集合3种任意一个对象
30
31 id object = [set3 anyObject];
32
33
34
35 //集合1中是否包含内容为2的对象
36
37 BOOL isContain = [set1 containsObject:@"2"];
38
39
40
41 //集合1与集合2是否存在相同元素的对象(存在交集)
42
43 BOOL isIntersect = [set1 intersectsSet:set2];
44
45
46
47 //集合1与集合2中的元素是否完全匹配
48
49 BOOL isEqual = [set1 isEqualToSet:set2];
50
51
52
53 //集合1是否为集合2的子集
54
55 BOOL isSubset = [set1 isSubsetOfSet:set2];
56
57
58
59 //向集合1增加对象
60
61 NSSet *appset1 = [set1 setByAddingObject:@"3"];
62
63 NSSet *appset2 = [set1 setByAddingObjectsFromSet:set2];
64
65 NSSet *appset3 = [set1 setByAddingObjectsFromArray:array1];
66
67
68
69 //NSMutableSe特有的方法
70
71
72
73 //创建一个空集合
74
75 NSMutableSet *mSet1 = [NSMutableSet set];
76
77 NSMutableSet *mSet2 = [NSMutableSet setWithObjects:@"1",@"2", nil];
78
79 NSMutableSet *mSet3 = [NSMutableSet setWithObjects:@"2",@"3", nil];
80
81
82
83 //集合2减去集合3中有的元素
84
85 [mSet2 minusSet:mSet3];
86
87
88
89 //集合2与集合3交集
90
91 [mSet2 intersectsSet:mSet3];
92
93
94
95 //集合2与集合3并集
96
97 [mSet2 unionSet:mSet3];
98
99
100
101 //将空集合1设置为集合3的内容
102
103 [mSet1 setSet:mSet3];
104
105
106
107 //根据数组内容删除集合中的对象
108
109 [mSet2 removeAllObjects];
110
111 [mSet2 removeObject:@"1"];
112
113 [mSet2 addObject:@"4"];