Reputation: 1060
I want to append an object in an array.
For example,
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<5; i++)
{
[array addObject:@[@"Any"]];
}
It gives an output like this:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any
),
(
Any
)
)
Now I want to append object at index 3 of an array so that it could appear like below:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any, Where, How, When
),
(
Any
)
)
Upvotes: 0
Views: 84
Reputation: 19024
If I understand you...
[array replaceObjectAtIndex:2 withObject:@[[[array objectAtIndex:2] firstObject],@"Where",@"How",@"When"]];
Upvotes: 1
Reputation: 1340
Try this:
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);
Note, @[]
creates an instance of NSArray
class which cannot be modified later. You can modify only instances of NSMutableArray
class.
Upvotes: 0
Reputation: 17186
Use function insertObjectAtIndex to achieve it.
[array insertObject:anObject atIndex:2];
Upvotes: 1