本文主要是介绍【Foundation-37-1】#import Foundation/NSIndexSet.h不可索引集合,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NSIndexSet 用来让你从某个 data structure 里面提取一部分东西出来成为一个新的东西。比如你有一个 NSArray, 里面是
(one, two, three, four, five)
然后你造了个 indexSet 的内容是 0,1,2,4
然后你把它套到那个 array 上,就是 (one, two,three,five)
@interface NSIndexSet :NSObject <NSCopying,NSMutableCopying, NSSecureCoding> {
@protected
【初始化】
+ (instancetype)indexSet;
+ (instancetype)indexSetWithIndex:(NSUInteger)value;
+ (instancetype)indexSetWithIndexesInRange:(NSRange)range;
- (instancetype)initWithIndexSet:(NSIndexSet *)indexSet NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithIndex:(NSUInteger)value;
- (instancetype)initWithIndexesInRange:(NSRange)range NS_DESIGNATED_INITIALIZER;
- (BOOL)isEqualToIndexSet:(NSIndexSet *)indexSet;// 比较
@property (readonly)NSUInteger count;// 数量
@property (readonly)NSUInteger firstIndex;// 第一个
@property (readonly)NSUInteger lastIndex;// 最后一个
- (NSUInteger)getIndexes:(NSUInteger *)indexBuffer maxCount:(NSUInteger)bufferSize inIndexRange:(NSRangePointer)range;// 阿西吧,待研究
- (NSUInteger)countOfIndexesInRange:(NSRange)range NS_AVAILABLE(10_5, 2_0);// 在(x,y)范围内的个数
【大于小于】
- (NSUInteger)indexGreaterThanIndex:(NSUInteger)value;// 大于value 的第一个位置
- (NSUInteger)indexLessThanIndex:(NSUInteger)value;// 小雨value 的第一个位置
- (NSUInteger)indexGreaterThanOrEqualToIndex:(NSUInteger)value;// 大于等于 value 的第一个位置
- (NSUInteger)indexLessThanOrEqualToIndex:(NSUInteger)value;// 小于等于 value 的第一个位置
【判断】
- (BOOL)containsIndex:(NSUInteger)value;// 是否包含 value
- (BOOL)containsIndexesInRange:(NSRange)range;// 是否包含 range
- (BOOL)containsIndexes:(NSIndexSet *)indexSet;// 是否包含 indexSet
- (BOOL)intersectsIndexesInRange:(NSRange)range;// 是否与 range 有交集
【遍历】
- (void)enumerateIndexesUsingBlock:(void (^)(NSUInteger idx,BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateIndexesWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(NSUInteger idx,BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateIndexesInRange:(NSRange)range options:(NSEnumerationOptions)opts usingBlock:(void (^)(NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
NSRange myRange = NSMakeRange(1, 4);NSIndexSet *indexset = [[NSIndexSet alloc]initWithIndexesInRange:myRange];[indexset enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {NSLog(@"%ld",idx);}];
【遍历符合条件的个数】
- (NSUInteger)indexPassingTest:(BOOL (^)(NSUInteger idx,BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUInteger idx,BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexInRange:(NSRange)range options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
NSArray *arr = @[@"0",@"1",@"2",@"3",@"4"];NSRange myRange = NSMakeRange(1, 4);NSIndexSet *indexset = [[NSIndexSet alloc]initWithIndexesInRange:myRange];NSUInteger test = [indexset indexPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {if ([[arr objectAtIndex:idx]isEqualToString:@"3"]) {NSLog(@"ok");//test +1;return YES;}else{NSLog(@"no");return NO;//结束}}];
【遍历符合条件的索引集合】
- (NSIndexSet *)indexesPassingTest:(BOOL (^)(NSUInteger idx,BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUInteger idx,BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesInRange:(NSRange)range options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
NSArray *arr = @[@"0",@"2",@"2",@"3",@"4"];NSRange myRange = NSMakeRange(1, 4);NSIndexSet *indexset = [[NSIndexSet alloc]initWithIndexesInRange:myRange];NSIndexSet *test1 = [indexset indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {if ([[arr objectAtIndex:idx]isEqualToString:@"2"]) {NSLog(@"ok");//test +1;return YES;}else{NSLog(@"no");return NO;//结束}}];
【遍历其中的所有范围】
- (void)enumerateRangesUsingBlock:(void (^)(NSRange range,BOOL *stop))block NS_AVAILABLE(10_7, 5_0);
- (void)enumerateRangesWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(NSRange range,BOOL *stop))block NS_AVAILABLE(10_7, 5_0);
- (void)enumerateRangesInRange:(NSRange)range options:(NSEnumerationOptions)opts usingBlock:(void (^)(NSRange range, BOOL *stop))block NS_AVAILABLE(10_7, 5_0);
NSMutableIndexSet *muindex = [NSMutableIndexSet indexSet];[muindex addIndex:1];[muindex addIndex:3];[muindex addIndex:4];[muindex addIndex:5];[muindex addIndex:5];[muindex addIndex:7];[muindex enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {NSLog(@"%lu",(unsigned long)range.location);NSLog(@"%lu",range.length);}];
@end
这篇关于【Foundation-37-1】#import Foundation/NSIndexSet.h不可索引集合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!