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?
When you
-addObject:therunScoreDataArraytohighScoresArray, it’s not copying the values in the array, it’s adding a reference to the actualrunScoreDataArrayto the parent array.So when you then go and clear out the
runScoreDataArraywith-removeAllObjects, that affects the reference inside the highScoresArray as well.Depending on what you’re after, you might want something like:
to insert a shallow copy.