NSSet和NSArry的区别&NSSet的一般用法


引用自:http://zhidao.baidu.com/link?url=uD25s5OdnfmnNzvEUcYl3eCuSNE20BytMTgTQ_L42YM3B0DWWCR3Fj3gBStI09GKBwJT1obbGSnPWgwDlr-7X1qByLjjn8VORAjMygfxJZ3
http://blog.csdn.net/ms2146/article/details/8657011

1.NSSet和NSArry的区别:

NSSet到底什么类型,其实它和NSArray功能性质一样,用于存储对象,属于集合; NSSet , NSMutableSet类声明编程接口对象,无序的集合,在内存中存储方式是不连续的,不像NSArray,NSDictionary(都是有序的集合)类声明编程接口对象是有序集合,在内存中存储位置是连续的;
NSSet和我们常用NSArry区别是:在搜索一个一个元素时NSSet比NSArray效率高,主要是它用到了一个算法hash(散列,也可直译为哈希);开发文档中这样解释:You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.
比如你要存储元素A,一个hash算法直接就能直接找到A应该存储的位置;同样,当你要访问A时,一个hash过程就能找到A存储的位置.而对于NSArray,若想知道A到底在不在数组中,则需要便利整个数组,显然效率较低了;
NSSet,NSArray都是类,只能添加cocoa对象,如果需要加入基本数据类型(int,float,BOOL,double等),需要将数据封装成NSNumber类型.

2.NSSet的一般用法


 
 
  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;  
  78. }  

日志:

[plain] view plaincopy
  1.  set1 :{(  
  2.     d,  
  3.     b,  
  4.     c,  
  5.     a  
  6. )}  
  7.  set2 :{(  
  8.     1,  
  9.     2,  
  10.     3  
  11. )}  
  12.  set3 :{(  
  13.     a,  
  14.     b,  
  15.     c  
  16. )}  
  17.  set1 count :4  
  18.  allObj :(  
  19.     1,  
  20.     2,  
  21.     3  
  22. )  
  23.  anyObj :a  
  24.  contains :0  
  25.  intersect obj :1  
  26.  isEqual :0  
  27.  isSubSet :1  
  28.  addFromArray :{(  
  29.     3,  
  30.     b,  
  31.     1,  
  32.     4,  
  33.     2,  
  34.     a  
  35. )}  
  36.  minus :{(  
  37.     1,  
  38.     3  
  39. )}  
  40.  intersect :{(  
  41.     1  
  42. )}  
  43.  union :{(  
  44.     b,  
  45.     1,  
  46.     2,  
  47.     a,  
  48.     c  
  49. )}  
  50.  removeObj :{(  
  51.     b,  
  52.     1,  
  53.     2,  
  54.     c  
  55. )}  
  56.  removeAll :{(  
  57. )}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值