开发者

how to copy array?

I have four arrays as follows:

toArray = [[NSMutableArray alloc] initWithObje开发者_StackOverflow社区cts:@"to 1",@"to 2",@"to 3",@"to 4",@"to 5",@"to 6",@"to 7",nil]; 
fromArray = [[NSMutableArray alloc] initWithObjects:@"from 1",@"from 2",@"from 3",@"from 4",@"from 5",@"from 6",@"from 7",nil]; 
messageArray = [[NSMutableArray alloc] initWithObjects:@"message 1",@"message 2",@"message 3",@"message 4",@"message 5",@"message 6",@"message 7",nil]; 
dayArray = [[NSMutableArray alloc] initWithObjects:@"day 1",@"day 2",@"day 3",@"day 4",@"day 5",@"day 6",@"day 7",nil];

I want to copy or create a single array which should contain all these 4 arrays. How can i achieve it?


Your question is a little ambiguous; how do you want the result?

NSMutableArray *completeArray = [NSMutableArray array];
[completeArray addObjectsFromArray:toArray];
[completeArray addObjectsFromArray:fromArray];
[completeArray addObjectsFromArray:messageArray];
[completeArray addObjectsFromArray:dayArray];

Or

NSMutableArray *completeArray  = [NSMutableArray arrayWithObjects:toArray, fromArray, messageArray, dayArray, nil];

Or if all arrays have the same number of elements:

NSMutableArray *completeArray = [NSMutableArray array];
for (NSUInteger i = 0; i < [toArray count]; i++)
{
    NSString *fullString = [NSString stringWithFormat:@"%@ %@ %@ %@", [toArray objectAtIndex:i], [fromArray objectAtIndex:i], [messageArray objectAtIndex:i], [dayArray objectAtIndex:i]];
    [completeArray addObject:fullString];
}

Or, as an array of dictionaries (also assuming all arrays are same length):

NSMutableArray *completeArray = [NSMutableArray array];
for (NSUInteger i = 0; i < [toArray count]; i++)
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
        [toArray objectAtIndex:i], @"to",
        [fromArray objectAtIndex:i], @"from",
        [messageArray objectAtIndex:i], @"message",
        [dayArray objectAtIndex:i], @"day",
        nil];
    [completeArray addObject:dict];
}


This should work:

NSArray *singleArray = [NSArray arrayWithElements:toArray, fromArray, messsageArray, dayArray, nil]; 
// mind the nil element at the end


NSMutableArray *result = [NSMutableArray arrayWithCapacity:0];

[result addObjectsFromArray:toArray];
[result addObjectsFromArray:fromArray];
[result addObjectsFromArray:messageArray];
[result addObjectsFromArray:dayArray];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜