views:

74

answers:

3

I have four arrays as follows:

toArray = [[NSMutableArray alloc] initWithObjects:@"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?

+1  A: 

This should work:

NSArray *singleArray = [NSArray arrayWithElements:toArray, fromArray, messsageArray, dayArray, nil]; 
// mind the nil element at the end
Eimantas
There's no method called `+arrayWithElements:`. Do you mean `+arrayWithObjects:`?
KennyTM
actually i need to show info like in this mannerto1from1 message1day1to2from2 message2day2to3from3 message3day3something like dat,in a table view.wat should i do to get this patter in table view.??thank Eimantas,for a quick reply regards
shishir.bobby
yes, i meant `+arrayWithObjects:`. I presume you're creating some sort of message list. You'll need an array with dictionaries where each dictionary would hold one message (i.e. represent one row for a table).
Eimantas
@eimantas,, can u plz guide me,how should i do this.regardsshishir
shishir.bobby
NOw now wen i got all three array in a single array,how can i set array into table view??showing something like disto1from1message1day1--------------to2from2message2day2--------------to3from3message3day4--------------like in this pattern????
shishir.bobby
read about NSTableViewDataSource protocol in apple documentation
Eimantas
ok Eimantas..thanks
shishir.bobby
+1  A: 
NSMutableArray *result = [NSMutableArray arrayWithCapacity:0];

[result addObjectsFromArray:toArray];
[result addObjectsFromArray:fromArray];
[result addObjectsFromArray:messageArray];
[result addObjectsFromArray:dayArray];
drawnonward
What is the reason for "arrayWithCapacity: 0"? Why not just "array"
JeremyP
i hv an array ("array A") in class "A".and in class "B",i m having another array ("array B"),which fills tableview,of class "B" only.i need to fill tableview,with the values of class A's array (i.e array A).how can i do it??how to copy array from another class ?i hope i m clear with my questionregards shishir
shishir.bobby
+1  A: 

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];
}
dreamlax
thank dreamlax....for showing me so many ways of achieving that...i really appreciate u.regardsshishir
shishir.bobby