Objective-C索引集NSIndexSet与可变索引集NSMutableIndexSet使用

//创建空array
    NSMutableArray *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,objective-c,前端)