Reputation: 563
I have an array like this:
array: (
(
"http://aaa/product/8_1371121323.png",
"http://aaa/product/14_1371123271.png"
),
(
"http://aaa/product/9_1371121377.png"
)
)
and I have to create another array from that one like this
array: (
"http://aaa/product/8_1371121323.png",
"http://aaa/product/14_1371123271.png",
"http://aaa/product/9_1371121377.png"
)
How can I do that? Is it possible to combine all the objects and separate them using some string?
Upvotes: 22
Views: 6850
Reputation: 18253
It can be done in a single line if you like key-value coding (KVC). The @unionOfArrays
collection operator does exactly what you are looking for.
You may have encountered KVC before in predicates, bindings and similar places, but it can also be called in normal Objective-C code like this:
NSArray *flatArray = [array valueForKeyPath: @"@unionOfArrays.self"];
There are other collection operators in KVC, all prefixed with an @
sign, as discussed here.
Upvotes: 114
Reputation: 2497
Sample code:
NSArray* arrays = @(@(@"http://aaa/product/8_1371121323.png",@"http://aaa/product/14_1371123271.png"),@(@"http://aaa/product/9_1371121377.png"));
NSMutableArray* flatArray = [NSMutableArray array];
for (NSArray* innerArray in arrays) {
[flatArray addObjectsFromArray:innerArray];
}
NSLog(@"%@",[flatArray componentsJoinedByString:@","]);
Upvotes: 1
Reputation: 27225
Sample Code :
NSMutableArray *mainArray = [[NSMutableArray alloc] init];
for (int i = 0; i < bigArray.count ; i++)
{
[mainArray addObjectsFromArray:[bigArray objectAtIndex:i]];
}
NSLog(@"mainArray :: %@",mainArray);
Upvotes: 6
Reputation: 21808
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:[initialArray objectAtIndex:0]];
[arr1 addObjectsFromArray:[initialArray objectAtIndex:1]];
Now arr1 contains all the objects
Upvotes: -1