iOS官方文档 Foundation篇---NSIndexSet

NSIndexSet

表示一个唯一的无符号整数的不可变集合,又称为索引集;继承自NSObject;
索引集中的对象是唯一的无符号整数,且不重复;意味着每个索引值只能在索引集中出现一次。
不建议使用索引集来存储任意整数值集合,因为索引集将索引存储为已排序的范围。比存储单个整数的集合更有效;

创建索引集
// 创建空索引集
NSIndexSet *indexSet = [NSIndexSet indexSet];

// 使用索引创建索引集
NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndex:1];//(1)

// 使用索引范围创建索引集
NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)];//(0-2)

// 使用索引集初始化已分配的索引对象
NSIndexSet *indexSet3 = [indexSet initWithIndexSet:indexSet1];//(1)
获取索引
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)];//(0-4)

// 获取索引集中的第一个索引
NSUInteger firstIndex = indexSet.firstIndex;//0

// 获取索引集中的最后一个索引
NSUInteger lastIndex = indexSet.lastIndex;//4

// 获取索引集中最接近的索引,该索引小于特定索引或未找到的指示符。
NSUInteger index1 = [indexSet indexLessThanIndex:1];//0

// 获取索引集中最接近的索引,该索引小于或等于特定索引或未找到的指示符
NSUInteger index2 = [indexSet indexLessThanOrEqualToIndex:0];//0

// 获取索引集中最接近的索引,该索引大于特定索引或未找到的指示符
NSUInteger index = [indexSet indexGreaterThanIndex:0];//1

// 获取索引集中最接近的索引,该索引大于或等于特定索引或未找到的指示符。
NSUInteger index3 = [indexSet indexGreaterThanOrEqualToIndex:0];//0
查询索引集
// 使用索引范围创建索引集
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)];//(0-4)
NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 3)];//(2-4)

// 索引集中是否包含指定索引
BOOL isContain = [indexSet containsIndex:0];//YES

// 索引集中是否包含指定索引集中的索引
BOOL isContain2 = [indexSet containsIndexes:indexSet1];//YES

// 索引集是否包含索引范围表示的索引
BOOL isContain1 = [indexSet containsIndexesInRange:NSMakeRange(0, 3)];//YES

// 索引集是否包含范围中的任何索引
BOOL isInter = [indexSet intersectsIndexesInRange:NSMakeRange(0, 3)];//YES

// 索引集中的索引数
NSUInteger count = indexSet.count;//5

// 索引集中指定范围的索引数。
NSUInteger count1 = [indexSet countOfIndexesInRange:NSMakeRange(0, 2)];//2

// 返回满足条件的索引
NSUInteger count2 = [indexSet indexPassingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {
    if (idx == 1) {
        return YES;
    }
    return NO;
}];//1
// 返回满足条件的索引集
NSIndexSet *indexSet2 = [indexSet indexesPassingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {
    if (idx < 2) {
        return YES;
    }
    return NO;
}];//(0-1)
// 根据遍历顺序返回满足条件的索引
NSUInteger count3 = [indexSet indexWithOptions:NSEnumerationConcurrent passingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {
    if (idx == 1) {
        return YES;
    }
    return NO;
}];//1

// 根据遍历顺序返回满足条件的索引集
NSIndexSet *indexSet3 = [indexSet indexesWithOptions:NSEnumerationConcurrent passingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {
    if (idx < 2) {
        return YES;
    }
    return NO;
}];//(0-1)
比较索引集
// 使用索引范围创建索引集
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)];//(0-4)
NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 3)];//(2-4)

// 两个集合的索引是否相同
BOOL isEqual = [indexSet isEqualToIndexSet:indexSet1];//NO
枚举索引集内容(遍历)
// 使用索引范围创建索引集
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)];//(0-4)

// 使用指定范围内的索引集中的每个对象执行给定块。
[indexSet enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
    NSLog(@"1-----%@",NSStringFromRange(range));
}];//(0-5)

// 使用指定范围内的索引集中的每个对象执行给定块
[indexSet enumerateRangesWithOptions:NSEnumerationConcurrent usingBlock:^(NSRange range,BOOL * _Nonnull stop) {
    NSLog(@"2-----%@",NSStringFromRange(range));//(0-5)
}];//(0-5)

// 使用块枚举对象范围内的范围
[indexSet enumerateRangesInRange:NSMakeRange(0, 3) options:NSEnumerationConcurrent usingBlock:^(NSRange range, BOOL * _Nonnull stop) {
    NSLog(@"3-----%@",NSStringFromRange(range));
}];//(0-3)

// 使用索引集中的每个对象执行给定的块
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
    NSLog(@"4-----%@",[NSString stringWithFormat:@"%ld",idx]);
}];//0,1,2,3,4

// 使用指定的枚举选项在索引集的索引上执行给定的块。
[indexSet enumerateIndexesWithOptions:NSEnumerationConcurrent usingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
    NSLog(@"5-----%@",[NSString stringWithFormat:@"%ld",idx]);
}];//0,1,2,3,4

// 使用指定的枚举选项,使用指定范围内的索引执行给定的块。
[indexSet enumerateIndexesInRange:NSMakeRange(0, 3) options:NSEnumerationConcurrent usingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
    NSLog(@"6-----%@",[NSString stringWithFormat:@"%ld",idx]);
}];//0,1,2

NSMutableIndexSet

表示一个唯一的无符号整数的可变集合,又称为可变索引集;继承自NSIndexSet;
可变索引集中的值始终排序,因此添加值的顺序无关紧要。

添加索引
// 使用索引范围创建索引集
NSMutableIndexSet *muIndexSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)];//(0-1)

// 添加指定索引
[muIndexSet addIndex:2];//(0-2)

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 1)];//(3)
// 将指定索引集中的索引添加到接收索引集
[muIndexSet addIndexes:indexSet];//(0-3)

// 将指定索引范围的索引添加到接收索引
[muIndexSet addIndexesInRange:NSMakeRange(4, 2)];//(0-5)
删除索引
NSMutableIndexSet *muIndexSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)];//(0-4)

// 删除指定索引
[muIndexSet removeIndex:0];//(1-4)

// 删除指定索引集中的索引
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)];
[muIndexSet removeIndexes:indexSet];//(3-4)

// 删除指定索引范围的索引
[muIndexSet removeIndexesInRange:NSMakeRange(0, 1)];//(3-4)

// 清空索引集
[muIndexSet removeAllIndexes];//()
移动索引
NSMutableIndexSet *muIndexSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)];//(0-4)
    
// 像左或向右移动索引(by:负数向左,正数向右)
[muIndexSet shiftIndexesStartingAtIndex:0 by:1];//(1-5)
欢迎留言指正,会持续更新。。。

你可能感兴趣的:(iOS官方文档 Foundation篇---NSIndexSet)