【集合】NSIndexSet

NSIndexSet类代表一个不变的独特的无符号整数的集合,称为索引,因为使用它们的方法此集合被称为索引集。索引必须在0--NSNotFound - 1范围内.

在您的代码中使用索引集来存储索引到一些其他的数据结构中。例如,给定一个NSArray对象,你可以用indexset来标识数组中的子集。
你不应该使用indexset来存储任意集合的整数值,因为索引集存储索引为排序范围。这使得它们比存储单个整数集合更有效。这也意味着每一个索引值只能在索引集合中出现一次。
初始化方法有
init
initwithIndexesInRange 
initWithIndexSet
绝不能子类化NSIndexSet 可变类为NSMutableIndexSet

api
创建对象
+ (instancetype)indexSet(空的)
+ (instancetype)indexSetWithIndex:(NSUInteger)index(包含有一个index的对象)
+ (instancetype)indexSetWithIndexesInRange:(NSRange)indexRange (包含有从indexRange中的所有的元素)
- (instancetype)init
- (instancetype)indexSet(空的)
- (instancetype)indexSetWithIndex:(NSUInteger)index(包含有一个index的对象)
- (instancetype)indexSetWithIndexesInRange:(NSRange)indexRange (包含有从indexRange中的所有的元素)
实践代码

NSIndexSet  *indexset = [NSIndexSet indexSet];

    NSIndexSet * indexSet2 = [NSIndexSetindexSetWithIndex:1];

    NSIndexSet * indexset3 = [NSIndexSetindexSetWithIndexesInRange:NSMakeRange(010)];

    [indexset3 enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {

        NSLog(@"%lu",(long)idx);

    }];

结果:

1. 空的

2.输出1

3.输出0-9


查询indexsets

- (BOOL)containsIndex:(NSUInteger)index

- (BOOL)containsIndexes:(NSIndexSet *)indexSet

- (BOOL)containsIndexesInRange:(NSRange)indexRange

- (BOOL)intersectsIndexesInRange:(NSRange)indexRange

@property(readonly) NSUInteger count

- (NSUInteger)countOfIndexesInRange:(NSRange)indexRange

- (NSUInteger)indexPassingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate

- (NSIndexSet *)indexesPassingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate

- (NSUInteger)indexWithOptions:(NSEnumerationOptions)optspassingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate

- (NSIndexSet *)indexesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUIntegeridx, BOOL *stop))predicate

- (NSUInteger)indexInRange:(NSRange)range options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUIntegeridx, BOOL *stop))predicate

- (NSIndexSet *)indexesInRange:(NSRange)range options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUIntegeridx, BOOL *stop))predicate

迭代器遍历

- (void)enumerateRangesInRange:(NSRange)range options:(NSEnumerationOptions)opts usingBlock:(void (^)(NSRangerange, BOOL *stop))block

- (void)enumerateRangesUsingBlock:(void (^)(NSRange range, BOOL *stop))block

- (void)enumerateRangesWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(NSRangerange, BOOL *stop))block

比较

- (BOOL)isEqualToIndexSet:(NSIndexSet *)indexSet

获取indexsets

@property(readonly) NSUInteger firstIndex

@property(readonly) NSUInteger lastIndex

- (NSUInteger)indexLessThanIndex:(NSUInteger)index

- (NSUInteger)indexLessThanOrEqualToIndex:(NSUInteger)index

- (NSUInteger)indexGreaterThanOrEqualToIndex:(NSUInteger)index

- (NSUInteger)indexGreaterThanIndex:(NSUInteger)index

- (NSUInteger)getIndexes:(NSUInteger *)indexBuffer maxCount:(NSUInteger)bufferSize inIndexRange:(NSRangePointer)indexRange

迭代Indexes

- (void)enumerateIndexesUsingBlock:(void (^)(NSUInteger idx, BOOL *stop))block

- (void)enumerateIndexesWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(NSUIntegeridx, BOOL *stop))block

- (void)enumerateIndexesInRange:(NSRange)range options:(NSEnumerationOptions)opts usingBlock:(void (^)(NSUIntegeridx, BOOL *stop))block

API列表如上 代码有空贴











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值