Objective-C语法之NSSet和NSMutableSet

来源:互联网 发布:数学在线解题软件 编辑:程序博客网 时间:2024/05/01 21:16

NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性。当插入相同的数据时,不会有任何效果。从内部实现来说是hash表,所以可以常数时间内查找一个数据。


1、NSSet的使用

[NSSet setWithSet:(NSSet *)set]; 用另外一个set对象构造
[NSSet setWithArray:(NSArray *)array];用数组构造
[NSSet setWithObjects:...]:创建集合对象,并且初始化集合中的数值,结尾必需使用nil标志。
[set count] ; 得到这个结合对象的长度。
[set containsObject:...]: 判断这个集合中是否存在传入的对象,返回Bool值。
[set objectEnumerator]: 将集合放入迭代器。
[enumerator nextObject]:得到迭代器中的下一个节点数据,使用while遍历这个迭代器,方可遍历集合对象中的对象。
[set isEqualToSet:objset]:判断两个集合是否完全相等,返回Bool值。
[set isSubsetOfSet:objset]:判断集合中的所有数据是否都相等与objeset集合中,返回Bool值。
[set allObjects];

示例代码:

1.1 以NSArray构造set

[cpp] view plaincopy
  1. NSArray *array = [[NSArray alloc] initWithObjects:@"对象abc",@"rongfzh", @"totogo2010",nil];  
  2.        NSSet *set3 = [NSSet setWithArray:array];  
  3.        NSLog(@"%@", set3);  

打印:

[objc] view plaincopy
  1. 2012-07-10 09:39:02.015 objectiveC[720:403] {(  
  2.     rongfzh,  
  3.     "\U5bf9\U8c61abc",  
  4.     totogo2010  
  5. )}  

1.2 set的一些比较方法的使用。

[cpp] view plaincopy
  1. int main(int argc, const char * argv[])  
  2. {  
  3.     @autoreleasepool {  
  4.         NSSet *set = [NSSet setWithObjects:@"25",@"age",@"张三",@"name",@"男",nil];  
  5.         NSSet *set1 = [NSSet setWithObjects:@"25",@"age",@"张三",@"name",@"男",@"性别",nil];  
  6.           
  7.         NSLog(@"set count:%lu", [set count]);  
  8.         //判断是否含有age字符串  
  9.         if([set containsObject:@"age"]) {  
  10.             NSLog(@"set包含age");  
  11.         }  
  12.         //判断set 是否等于set1  
  13.         if ([set isEqualToSet:set1]) {  
  14.             NSLog(@"set 等于 set1");  
  15.         }  
  16.         //判断set是否是否是set1的子集合  
  17.         if ([set isSubsetOfSet:set1]) {  
  18.             NSLog(@"set isSubsetOfSet set1");  
  19.         }  
  20.         //获取所有set对象  
  21.         NSArray *array = [set allObjects];  
  22.         NSLog(@"array:%@", array);  
  23.           
  24.         //迭代遍历  
  25.         NSEnumerator *enumerator = [set objectEnumerator];  
  26.         for (NSObject *object in enumerator) {  
  27.             NSLog(@"set1里的对象:%@", object);  
  28.         }  
  29.     }  
  30.     return 0;  
  31. }  

打印结果:

[objc] view plaincopy
  1. 2012-07-10 09:50:32.018 objectiveC[939:403] set count:5  
  2. 2012-07-10 09:50:32.020 objectiveC[939:403] set包含age  
  3. 2012-07-10 09:50:32.021 objectiveC[939:403] set isSubsetOfSet set1  
  4. 2012-07-10 09:50:32.023 objectiveC[939:403] array:(  
  5.     age,  
  6.     25,  
  7.     "\U7537",  
  8.     "\U5f20\U4e09",  
  9.     name  
  10. )  
  11. 2012-07-10 09:50:32.027 objectiveC[939:403] set1里的对象:age  
  12. 2012-07-10 09:50:32.028 objectiveC[939:403] set1里的对象:25  
  13. 2012-07-10 09:50:32.028 objectiveC[939:403] set1里的对象:男  
  14. 2012-07-10 09:50:32.029 objectiveC[939:403] set1里的对象:张三  
  15. 2012-07-10 09:50:32.029 objectiveC[939:403] set1里的对象:name  

2、NSMutableSet的使用

NSMutableSet继承NSSet,它可以使用NSSet的方法。

[NSMutableSet setWithCapacity:6]:创建可变集合对象,并且初始化长度为6。
[set addObject: obj] : 向集合中动态的添加对象。
[set removeObject:obj]:删除集合中的一个对象。
[set removeAllObjects]:删除集合中的所有对象。
[set unionSet:obj]:向集合中添加一个obj集合的所有数据。
[set minusSet:obj]:向集合中删除一个obj集合的所有数据。
[set intersectSet]:向集合中删除一个不包含obj集合的所有数据。

示例代码:

[cpp] view plaincopy
  1. int main(int argc, const char * argv[])  
  2. {  
  3.     @autoreleasepool {  
  4.         NSMutableSet *muSet = [NSMutableSet setWithCapacity:6];  
  5.         [muSet addObject:@"对象1"];  
  6.         NSSet *set = [NSSet setWithObjects:@"对象2",@"对象3", @"被企鹅咬了一口", nil];  
  7.         //添加set数据  
  8.         [muSet unionSet:set];  
  9.         for (NSObject *object in muSet) {  
  10.             NSLog(@"all nuSet:%@",object);  
  11.         }  
  12.         NSSet *set1 = [NSSet setWithObjects:@"对象2",@"对象3", nil];  
  13.           
  14.         //在muSet中删除包含set1总数据  
  15.         [muSet minusSet:set1];  
  16.         for (NSObject *object in muSet) {  
  17.             NSLog(@"after minusSet:%@",object);  
  18.         }  
  19.               
  20.     }  
  21.     return 0;  
  22. }  

打印结果:

[objc] view plaincopy
  1. 2012-07-10 10:09:08.194 objectiveC[1156:403] all nuSet:对象1  
  2. 2012-07-10 10:09:08.196 objectiveC[1156:403] all nuSet:被企鹅咬了一口  
  3. 2012-07-10 10:09:08.196 objectiveC[1156:403] all nuSet:对象2  
  4. 2012-07-10 10:09:08.197 objectiveC[1156:403] all nuSet:对象3  
  5. 2012-07-10 10:09:08.198 objectiveC[1156:403] after minusSet:对象1  
  6. 2012-07-10 10:09:08.198 objectiveC[1156:403] after minusSet:被企鹅咬了一口  
0 0
原创粉丝点击