数组分隔成两个一组

NSMutableArray *sourceM = [sourceArr mutableCopy];
    NSInteger count = sourceArr.count / 4; // 9个 三组 4 4 1
    
    NSMutableArray *temp = [NSMutableArray array];
    
    for (NSInteger i = 0; i < count; i++) {
        NSInteger from = 4*i - 1 > 0 ? 4*i - 1 : 0;
        NSArray *arr = [sourceM subarrayWithRange:NSMakeRange(from, 4)];
        [temp addObject:arr];
    }
    // 切出余数
    
    NSInteger leftCount = sourceArr.count - count * 4;
    if (leftCount > 0) {
        NSArray *leftArr = [sourceM subarrayWithRange:NSMakeRange(count * 4, leftCount)];
        [temp addObject:leftArr];
    }
    
    NSArray *resultArr = [temp copy];

 

posted on 2019-03-13 11:41  土匪7  阅读(395)  评论(0编辑  收藏  举报