iOS - NSIndexSet和NSMutableIndexSet

一. 首先明確這個類是幹嗎的 web

蘋果官方文檔中: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. 數組

這個類表明了一個惟一的無符號整數的集合,稱爲索引集合。其實就是索引組成的集合。 this

二. 建立NSIndexSet對象 spa

// 根據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);

控制檯輸出: code

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

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

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

上面的是類方法,也有對象方法初始化NSIndexSet對象的。具體的使用時看一下官方文檔或SDK均可以。

三 .  查詢索引集合 Querying Index Sets three

3.1  ci

- (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
相關文章
相關標籤/搜索