可变数组添加数组到指定位置- (void)insertObjects:(NSArray<ObjectType> *)objects atIndexes:(NSIndexSet *)indexes;
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 3; i++) {
[array addObject:[NSString stringWithFormat:@"%@", @(i)]];
}
NSLog(@"---------------- 1");
for (NSString *value in array) {
NSLog(@"value = %@", value);
}
NSArray *arrayTmp = @[@"devZhang", @"iOSZhang"];
// NSMakeRange(1, 2):1表示要插入的位置,2表示插入数组的个数
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)];
[array insertObjects:arrayTmp atIndexes:indexSet];
NSLog(@"---------------- 2");
for (NSString *value in array) {
NSLog(@"value = %@", value);
}
arrayTmp = @[@"国寿", @"汇安达", @"远光软件", @"惠卡世纪"];
NSInteger index = [array indexOfObject:@"2"];
indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index + 1, 4)];
[array insertObjects:arrayTmp atIndexes:indexSet];
NSLog(@"---------------- 3");
for (NSString *value in array) {
NSLog(@"value = %@", value);
}
2018-08-30 11:37:31.873428+0800 demo[9796:127799] ---------------- 1
2018-08-30 11:37:31.873629+0800 demo[9796:127799] value = 0
2018-08-30 11:37:31.873824+0800 demo[9796:127799] value = 1
2018-08-30 11:37:31.873980+0800 demo[9796:127799] value = 2
2018-08-30 11:37:31.874151+0800 demo[9796:127799] ---------------- 2
2018-08-30 11:37:31.874310+0800 demo[9796:127799] value = 0
2018-08-30 11:37:31.874469+0800 demo[9796:127799] value = devZhang
2018-08-30 11:37:31.874587+0800 demo[9796:127799] value = iOSZhang
2018-08-30 11:37:31.874741+0800 demo[9796:127799] value = 1
2018-08-30 11:37:31.874945+0800 demo[9796:127799] value = 2
2018-08-30 11:37:31.875185+0800 demo[9796:127799] ---------------- 3
2018-08-30 11:37:31.875453+0800 demo[9796:127799] value = 0
2018-08-30 11:37:31.875679+0800 demo[9796:127799] value = devZhang
2018-08-30 11:37:31.876041+0800 demo[9796:127799] value = iOSZhang
2018-08-30 11:37:31.876368+0800 demo[9796:127799] value = 1
2018-08-30 11:37:31.876695+0800 demo[9796:127799] value = 2
2018-08-30 11:37:31.877034+0800 demo[9796:127799] value = 国寿
2018-08-30 11:37:31.877412+0800 demo[9796:127799] value = 汇安达
2018-08-30 11:37:31.877781+0800 demo[9796:127799] value = 远光软件
2018-08-30 11:37:31.878828+0800 demo[9796:127799] value = 惠卡世纪