NSIndexSet基本用法

jc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. int main(int argc, const charchar * argv[]) {  
  4.     @autoreleasepool {  
  5.         /* 索引集合,可以用于数组多元素的提取 */  
  6.         NSIndexSet *indexSet1 = [[NSIndexSet alloc] initWithIndex: 1];  
  7.         NSIndexSet *indexSet2 = [[NSIndexSet alloc] initWithIndexSet: indexSet1];  
  8.         NSIndexSet *indexSet3 = [[NSIndexSet alloc] initWithIndexesInRange: NSMakeRange(23)];  
  9.         NSLog(@"indexSet1:%@", indexSet1);  
  10.         NSLog(@"indexSet2:%@", indexSet2);  
  11.         NSLog(@"indexSet3:%@", indexSet3);  
  12.           
  13.         /* 快速初始化数组 */  
  14.         NSArray *arr1 = @[@"One"@"Two thing"@"three"@"four test"@"Five"@"six"];  
  15.           
  16.         /* 提取连续下标子数组 */  
  17.         NSArray *arr2 = [arr1 objectsAtIndexes: indexSet3];  
  18.         NSLog(@"arr1:%@", arr1);  
  19.         NSLog(@"arr2:%@", arr2);  
  20.           
  21.         /* 提取离散下标数组.需要注意的是,index集合是从小到大排序 */  
  22.         NSMutableIndexSet *mSet3 = [[NSMutableIndexSet alloc] init];  
  23.         [mSet3 addIndex: 2];  
  24.         [mSet3 addIndex: 0]; /* 即使先加入2后加入0,也是从小到大按序排列,从数组打印可以看出 */  
  25.         NSLog(@"mSet3:%@", mSet3);  
  26.         NSArray *arr3 = [arr1 objectsAtIndexes: mSet3];  
  27.         NSLog(@"arr3:%@", arr3);  
  28.     }  
  29.     return 0;  
  30. }  

输出结果:

[objc]  view plain  copy
  1. 2015-11-25 22:21:51.495 TestNSIndexSet[493:14234] indexSet1:<NSIndexSet: 0x1002068b0>[number of indexes1 (in 1 ranges), indexes: (1)]  
  2. 2015-11-25 22:21:51.496 TestNSIndexSet[493:14234] indexSet2:<NSIndexSet: 0x100206910>[number of indexes1 (in 1 ranges), indexes: (1)]  
  3. 2015-11-25 22:21:51.496 TestNSIndexSet[493:14234] indexSet3:<NSIndexSet: 0x1002069b0>[number of indexes3 (in 1 ranges), indexes: (2-4)]  
  4. 2015-11-25 22:21:51.497 TestNSIndexSet[493:14234] arr1:(  
  5.     One,  
  6.     "Two thing",  
  7.     three,  
  8.     "four test",  
  9.     Five,  
  10.     six  
  11. )  
  12. 2015-11-25 22:21:51.497 TestNSIndexSet[493:14234] arr2:(  
  13.     three,  
  14.     "four test",  
  15.     Five  
  16. )  
  17. 2015-11-25 22:21:51.497 TestNSIndexSet[493:14234] mSet3:<NSMutableIndexSet: 0x100208410>[number of indexes2 (in 2 ranges), indexes: (0 2)]  
  18. 2015-11-25 22:21:51.497 TestNSIndexSet[493:14234] arr3:(  
  19.     One,  
  20.     three  
  21. )  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值