iOS - NSIndexSet和NSMutableIndexSet

一. 首先明确这个类是干嘛的

苹果官方文档中:The NSIndexSet class represents an immutable collection of unique unsigned integers, known as indexesbecause of the way they are used. This collection is referred to as an index set. Indexes must be in the range 0 .. NSNotFound - 1.

这个类代表了一个唯一的无符号整数的集合,称为索引集合。其实就是索引组成的集合。

二. 创建NSIndexSet对象

// 根据range中的值创建索引数组
	NSRange range = NSMakeRange(5, 6);
	NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
	NSLog(@"range:%@, indexSet:%@", NSStringFromRange(range), indexSet);
	// 根据索引创建索引数组
	NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndex:8];
	NSLog(@"indexSet2:%@", indexSet2);
	//创建一个empty IndexSet
	NSIndexSet *emptyIndexSet = [NSIndexSet indexSet];
	NSLog(@"emptyIndexSet:%@", emptyIndexSet);

控制台输出:

 range:{5, 6}, indexSet:<NSIndexSet: 0x7feeb0510fe0>[number of indexes: 6 (in 1 ranges), indexes: (5-10)]

 indexSet2:<_NSCachedIndexSet: 0x7feeb0510f80>[number of indexes: 1 (in 1 ranges), indexes: (8)]

 emptyIndexSet:<_NSCachedIndexSet: 0x7feeb0510fc0>(no indexes)

上面的是类方法,也有对象方法初始化NSIndexSet对象的。具体的使用时看一下官方文档或SDK都可以。

三 .  查询索引集合 Querying Index Sets

3.1 

- (BOOL)containsIndex:(NSUInteger)value;是否包含索引value

- (BOOL)containsIndexesInRange:(NSRange)range;是否包含range描述的索引

- (BOOL)containsIndexes:(NSIndexSet *)indexSet;是否包含索引集合。子集的关系。

这三个方法很好理解。

3.2

- (BOOL)intersectsIndexesInRange:(NSRange)range;是否包含range描述的集合中的任何一个,只要包含一个就返回YES,与上面第二个方法对照。简单说就是俩个索引集合是否相交。

3.3 count 属性,索引集合的索引个数。

- (NSUInteger)countOfIndexesInRange:(NSRange)range 在索引集合中又在给定的range范围内的索引个数。

3.4 - (NSUInteger)indexPassingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate,相当于遍历索引集合中的元素,一旦返回YES就停止遍历。如果满足idx == indexSet.lastIndex返回yes,那么就不会越界遍历了,但这样返回值就是lastIndex了。

// return NO 不会停止,都走一遍,最后返回值是不确定的。 如果直接 return YES,那么,返回第一个值,并且索引集合中其他值不会再走了。如果满足条件 return YES,那么返回第一个满足条件的值,并且索引集合中其他值不会再走了。
	NSUInteger passingTest1 = 	[indexSet indexPassingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {
			NSLog(@"%lu, stop : %hhd", idx, stop );
		if (idx > 7) {// 满足条件
			return YES;
		}

		return NO;
		}];//一般这么用
	NSLog(@"passingTest1: %lu", (unsigned long)passingTest1);


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

//跟上面一样,遍历indexesset中的索引,只不过返回的是所有满足条件的都返回了。不论return什么,把indexSet中的索引元素全部遍历一次。return YES, 返回的NSIndexSet对象还是原对象indexSet的内容。 满足条件返回YES,满足条件的都放到返回的索引集合中。 返回NO的话,但是最后得到一个empty indexSet。
	NSIndexSet *passingTests1 = [indexSet indexesPassingTest:
								^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {
									NSLog(@"%lu, stop : %hhd", idx, stop );
									static NSInteger num = 0;
									num++;
									NSLog(@"num: %ld", (long)num);
									if (idx > 6) {
										return YES;
									}

									return NO;
								}];
	NSLog(@"passingTest1 :%@", passingTests1);



上面俩个方法中的参数 predicate其实就是一个block对象,而且是一个有返回值的block对象,可以直接实现了,也可以定义一个同类型的block,把定义好的block若为实参传过去。


3.6 - (NSUInteger)indexWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate


// NSEnumerationOptions  枚举值 表示列举的方式
//		NSEnumerationOptions unumerationOptions = NSEnumerationConcurrent;//同时的
		NSEnumerationOptions unumerationOptions = NSEnumerationReverse;// 按逆序,相反地
		[indexSet indexWithOptions:unumerationOptions
					   passingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {
						   NSLog(@"idx :%lu", idx);
						   return YES;}
		 ];// 这里面使用NSEnumerationConcurrent不明白有啥用



当opt传nil时,和3.4的方法一样。传NSenumerationConcurrent不明白有啥用,传Reverse是逆序遍历。

3.7. - (NSIndexSet *)indexesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate

参照3.5和3.7方法使用。

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

遍历范围:在NSIndexSet对象里的range这个范围里面的索引值 。返回值:在遍历范围里面通过了后面的block的第一个满足条件的索引值。那么什么叫通过了block,就是block返回YES的。

3.9

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

参照3.8 遍历范围:在NSIndexSet对象里的range这个范围里面的索引值 。返回值:在遍历范围里面通过了后面的block的所有满足条件的索引值。那么什么叫通过了block,就是block返回YES的。

四.列举IndexSet中的内容 Enumerating Index Set Content

这三个方法都满足下面2条:

If the Block parameter is nil this method will raise an exception.

This method executes synchronously.

也就是说block必须实现,方法是同步执行的。

 The following three convenience methods allow you to enumerate the indexes in the receiver by ranges of contiguous indexes. The performance of these methods is not guaranteed to be any better than if they were implemented with enumerateIndexesInRange:options:usingBlock:. However, depending on the receiver's implementation, they may perform better than that。

 If the specified range for enumeration intersects a range of contiguous indexes in the receiver, then the block will be invoked with the intersection of those two ranges.

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

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

- (void)enumerateRangesInRange:(NSRange)range options:(NSEnumerationOptions)opts usingBlock:(void (^)(NSRange range, BOOL *stop))block NS_AVAILABLE(10_7, 5_0);

五. 比较IndexSets comparing Index Sets

- (BOOL)isEqualToIndexSet:(NSIndexSet *)indexSet;

相同返回YES,不同返回NO.

六. 获取索引 Getting Indexes

@property (readonly) NSUInteger firstIndex;

@property (readonly) NSUInteger lastIndex;

- (NSUInteger)indexGreaterThanIndex:(NSUInteger)value;

- (NSUInteger)indexLessThanIndex:(NSUInteger)value;

- (NSUInteger)indexGreaterThanOrEqualToIndex:(NSUInteger)value;

- (NSUInteger)indexLessThanOrEqualToIndex:(NSUInteger)value;

/* Fills up to bufferSize indexes in the specified range into the buffer and returns the number of indexes actually placed in the buffer; also modifies the optional range passed in by pointer to be "positioned" after the last index filled into the buffer.Example: if the index set contains the indexes 0, 2, 4, ..., 98, 100, for a buffer of size 10 and the range (20, 80) the buffer would contain 20, 22, ..., 38 and the range would be modified to (40, 60).*/

- (NSUInteger)getIndexes:(NSUInteger *)indexBuffer maxCount:(NSUInteger)bufferSize inIndexRange:(nullable NSRangePointer)range;这个没懂

七.遍历索引集合 Enumerating Indexes

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

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

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

四和七到底有什么关系,又分别实现了什么呢?

八.  NSMutableIndexSet






转载于:https://my.oschina.net/u/2560887/blog/602090

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值