我想在数组中附加一个对象。
例如,
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<5; i++)
{
[array addObject:@[@"Any"]];
}
它提供如下输出:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any
),
(
Any
)
)
现在我想在数组的索引3处附加对象,以便它可以如下所示:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any, Where, How, When
),
(
Any
)
)
答案 0 :(得分:1)
使用函数insertObjectAtIndex来实现它。
[array insertObject:anObject atIndex:2];
答案 1 :(得分:1)
如果我理解你......
[array replaceObjectAtIndex:2 withObject:@[[[array objectAtIndex:2] firstObject],@"Where",@"How",@"When"]];
答案 2 :(得分:0)
试试这个:
NSMutableArray *array = [NSMutableArray new];
for (int i=0; i<5; i++)
{
[array addObject:[NSMutableArray arrayWithObject:@"Any"]];
}
NSLog(@"%@", array);
NSMutableArray *subArray = [array objectAtIndex:3];
[subArray addObjectsFromArray:@[@"Where", @"How", @"When"]];
NSLog(@"%@", array);
注意,@[]
创建了NSArray
类的实例,以后无法修改。您只能修改NSMutableArray
类的实例。