本文主要是介绍Objective-C索引集NSIndexSet与可变索引集NSMutableIndexSet使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//创建空arrayNSMutableArray *emptyArray = [NSMutableArray array];[emptyArray addObjectsFromArray:[marr subarrayWithRange:NSMakeRange(6, 5)]];[marr removeAllObjects];NSLog(@"%@",emptyArray);//创建索引集NSIndexSet *indexs = [NSIndexSet indexSetWithIndex:1];//指定索引范围indexs = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)];//判断索引是否存在BOOL isContain = [indexs containsIndex:0];//枚举索引集索引范围[indexs enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {NSLog(@"%@",NSStringFromRange(range));}];//遍历索引集所有索引[indexs enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {NSLog(@"%lu",(unsigned long)idx);}];NSLog(@"%@",emptyArray);//使用索引集删除多个元素[emptyArray removeObjectsAtIndexes:indexs];NSLog(@"%@",emptyArray);//可变索引集NSMutableIndexSet *miSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 100)];//向索引集添加索引[miSet addIndex:100];//创建范围索引集NSIndexSet *tmpset =[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(101,5)];//添加范围索引到索引集[miSet addIndexes:tmpset];//删除指定索引[miSet removeIndex:100];//删除指定范围索引[miSet removeIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(10, 30)]];NSLog(@"%@",miSet);//遍历可扩展索引集范围[miSet enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {NSLog(@"%@",NSStringFromRange(range));NSLog(@"location:%lu,length:%lu",range.location,range.length);}];
这篇关于Objective-C索引集NSIndexSet与可变索引集NSMutableIndexSet使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!