views:

190

answers:

1

I have an Array that contains array objects. An array of Arrays. When I apply the description method to the Array, I don't see the data in the inner arrays. Here is the code:

[runScoreDataArray addObject:[NSString stringWithString:currentUser]];
[runScoreDataArray addObject:[NSNumber numberWithDouble:mainScores.scoreTotal]];

NSLog(@"Array1 contains: %@", [runScoreDataArray description]);

// Now add the array runScoreDataArray to the Array highScoresArray
[highScoresArray addObject: runScoreDataArray];

// Empty the runScoresData Array after each run.
[runScoreDataArray removeAllObjects];

NSLog(@"Array2 contains: %@", [highScoresArray description]);

The NSLog printout for runScoresDataArray reads as it should: Array1 contains: (USER1,34500)

The NSLog for highScoresArray reads: ARRAY2 contains: ((),(),())

I was expecting to see the data for each array element in the NSLog printout, rather than the empty brackets.

Question: How can I use the description method to printout the contents of an array of arrays?

+4  A: 

When you -addObject: the runScoreDataArray to highScoresArray, it's not copying the values in the array, it's adding a reference to the actual runScoreDataArray to the parent array.

So when you then go and clear out the runScoreDataArray with -removeAllObjects, that affects the reference inside the highScoresArray as well.

Depending on what you're after, you might want something like:

[highScoresArray addObject: [NSArray arrayWithArray:runScoreDataArray]];

to insert a shallow copy.

quixoto