NSSet使用小结

来源:互联网 发布:超人软件 编辑:程序博客网 时间:2024/05/27 00:50
  1. #import <Foundation/Foundation.h>  
  2.   
  3.   
  4. int main(int argc, const char * argv[])  
  5. {  
  6.   
  7.     @autoreleasepool {  
  8.           
  9.         NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];  
  10.         NSSet *set2 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil];  
  11.         NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];  
  12.         NSSet *set3 = [NSSet setWithArray:array];  
  13.           
  14.         NSLog(@"set1 :%@", set1);  
  15.         NSLog(@"set2 :%@", set2);  
  16.         NSLog(@"set3 :%@", set3);  
  17.           
  18.         //获取集合个数  
  19.         NSLog(@"set1 count :%d", set1.count);  
  20.           
  21.         //以数组的形式获取集合中的所有对象  
  22.         NSArray *allObj = [set2 allObjects];  
  23.         NSLog(@"allObj :%@", allObj);  
  24.           
  25.         //获取任意一对象  
  26.         NSLog(@"anyObj :%@", [set3 anyObject]);  
  27.           
  28.         //是否包含某个对象  
  29.         NSLog(@"contains :%d", [set3 containsObject:@"obj2"]);  
  30.           
  31.           
  32.         //是否包含指定set中的对象  
  33.         NSLog(@"intersect obj :%d", [set1 intersectsSet:set3]);  
  34.           
  35.         //是否完全匹配  
  36.         NSLog(@"isEqual :%d", [set2 isEqualToSet:set3]);  
  37.           
  38.         //是否是子集合  
  39.         NSLog(@"isSubSet :%d", [set3 isSubsetOfSet:set1]);  
  40.           
  41.           
  42.           
  43.         NSSet *set4 = [NSSet setWithObjects:@"a", @"b", nil];  
  44.         NSArray *ary = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];  
  45.         NSSet *set5 = [set4 setByAddingObjectsFromArray:ary];  
  46.         NSLog(@"addFromArray :%@", set5);  
  47.           
  48.           
  49.           
  50.           
  51.         NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1", @"2", @"3", nil];  
  52.         NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"a", @"2", @"b", nil];  
  53.         NSMutableSet *mutableSet3 = [NSMutableSet setWithObjects:@"1", @"c", @"b", nil];  
  54.           
  55.         //集合元素相减  
  56.         [mutableSet1 minusSet:mutableSet2];  
  57.         NSLog(@"minus :%@", mutableSet1);  
  58.           
  59.         //只留下相等元素  
  60.         [mutableSet1 intersectSet:mutableSet3];  
  61.         NSLog(@"intersect :%@", mutableSet1);  
  62.           
  63.         //合并集合  
  64.         [mutableSet2 unionSet:mutableSet3];  
  65.         NSLog(@"union :%@", mutableSet2);  
  66.           
  67.         //删除指定元素  
  68.         [mutableSet2 removeObject:@"a"];  
  69.         NSLog(@"removeObj :%@", mutableSet2);  
  70.           
  71.           
  72.         //删除所有数据  
  73.         [mutableSet2 removeAllObjects];  
  74.         NSLog(@"removeAll :%@", mutableSet2);  
  75.           
  76.     }  
  77.     return 0;  
0 0