可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
Sample Code :
NSMutableArray *mainArray = [[NSMutableArray alloc] init]; for (int i = 0; i
回答2:
It can be done in a single line if you don't mind using key-value coding (KVC). The @unionOfArrays
collection operator does exactly what you are looking for.
You may have encountered KVC most often in predicates, bindings and similar places. However, it can 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. There is more about it in the docs.
回答3:
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:@","]);
回答4:
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:[initialArray objectAtIndex:0]]; [arr1 addObjectsFromArray:[initialArray objectAtIndex:1]];
Now arr1 contains all the objects