美文网首页
NSIndexSet整数集合

NSIndexSet整数集合

作者: 黄定师 | 来源:发表于2019-05-28 01:08 被阅读0次

前言

NSIndexSet是个无符号整数集合,该集合中的元素不可变且不可重复的。因为常被用来记录索引,所以就被叫做:索引集合。

NSMutableIndexSet,顾名思义是可变的索引集合,即可以对该集合的元素进行添加,删除等操作。

NSIndexSet简单示例

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 
    // 创建索引集合(0-99)
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 100)];
    // 判断索引集合是否包含某个索引
    [self indexSet:indexSet containsIndex:50];
    [self indexSet:indexSet containsIndex:200]; 
}

- (void)indexSet:(NSIndexSet *)indexSet containsIndex:(NSInteger)index {
    if ([indexSet containsIndex:index]) {
        HLog(@"%@   包含  %ld",indexSet,index);
    } else {
        HLog(@"%@   不包含 %ld",indexSet,index);
    }
}

// 结果
ViewController.m:43 <NSIndexSet: 0x133d98df0>[number of indexes: 100 (in 1 ranges), indexes: (0-99)]   包含  50
ViewController.m:45 <NSIndexSet: 0x133d98df0>[number of indexes: 100 (in 1 ranges), indexes: (0-99)]   不包含 200
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 创建可变索引集合
    NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
    [indexSet addIndex:1];
    [indexSet addIndexesInRange:NSMakeRange(3, 3)];
    [indexSet addIndexesInRange:NSMakeRange(7, 2)];
    
    // 原始数据
    NSArray *dataArr =@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    
    // 根据下表获取数据(注意数组越界问题)
    NSArray *result = [dataArr objectsAtIndexes:indexSet];
    [result enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,BOOL *stop) {
        HLog(@"%@",obj);
    }];
}

// 结果
ViewController.m:46 1
ViewController.m:46 3
ViewController.m:46 4
ViewController.m:46 5
ViewController.m:46 7
ViewController.m:46 8

参考文献

NSIndexSet
iOS取值之NSIndexset

相关文章

  • NSIndexSet整数集合

    前言 NSIndexSet是个无符号整数集合,该集合中的元素不可变且不可重复的。因为常被用来记录索引,所以就被叫做...

  • NSIndexSet-入门浅析

    1.NSIndexSet是什么? NSIndexSet 是个无符号整数集合。集合中的元素不可变的、不可重复。常被...

  • NSIndexSet 用法

    NSIndexSet类代表一个不可变的独特的无符号整数的集合,称为索引,因为使用它们的方式。这个集合被称为索引集。...

  • object-c 基础十八 【NSIndexSet】集合(无序的

    1、创建一个下标集合 NSIndexSet *inde = [[NSIndexSet alloc] initWit...

  • iOS官方文档 Foundation篇---NSIndexSet

    NSIndexSet 表示一个唯一的无符号整数的不可变集合,又称为索引集;继承自NSObject;索引集中的对象是...

  • NSIndexSet译

    文档 起语 希望听到不一样的声音 概况 NSIndexSet类代表由无符号整数组成的不可变集合。元素的范围是从0到...

  • 集合和数组

    先进行定义比较:NSIndexSet是排好序的,无重复的无符号整形集合.NSSet用于存储对象的集合;NSSet ...

  • 6.整数集合

    整数集合 1. 整数集合的实现 整数集合是Redis用于保存整数值的集合抽象数据结构,它可以保存类型为int16_...

  • 整数集合

    整数集合是用在我们在存储value集合,其中value是整数,当然数量过多的时候还是会变成链表。 在intset....

  • 整数集合

    整数集合(intset)是集合键的底层实现之一,当一个集合只包含整数值元素,并且这个集合的元素数量不多时,redi...

网友评论

      本文标题:NSIndexSet整数集合

      本文链接:https://www.haomeiwen.com/subject/zhsktctx.html