Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Stephen Hancocks](https://uploads.teamtreehouse.com/production/profile-photos/134900/micro_1233492_10153210736335422_423015326_n.jpg)
Stephen Hancocks
9,271 PointsIs addObject overwriting the rest of my array?
I have a number of arrays and I want to combine them. There may be an easier way to what i have done but please bear with me. I'd like to know what's going on here.
When I run the code below all of the NSLog's within the while block appear correct - i counts up sequential, the properties change as expected, the array increases in size etc.
However, when I run the final NSLog to print the Array it shows all properties for each of the 20 objects as the same. They are all the same as the last object added. Is there something fundamental I am missing?
Thanks
NSMutableArray *combinedArray = [[NSMutableArray alloc]initWithCapacity:0];
CombinedArrayClass *objectForCombinedArray = [[CombinedArrayClass alloc]init];
int i = 0;
while (i < [_array1 count]){
[combinedCaseCredits addObject:objectForCombinedArray];
objectForCombinedArray.month = [[_array1 objectAtIndex:i] valueForKey:@"month"];
NSLog(@"month = %@", [[_array1 objectAtIndex:i]valueForKey:@"month"]);
objectForCombinedArray.property1 = [[_array1 objectAtIndex:i] valueForKey:@"dataType1"];
objectForCombinedArray.property2 = [[_array2 objectAtIndex:i] valueForKey:@"dataType2"];
objectForCombinedArray.property3 = [[_array3 objectAtIndex:i] valueForKey:@"dataType3"];
NSLog(@"i = %i", i);
NSLog(@"array length = %lu", (unsigned long)[combinedCaseCredits count]);
i++;
}
_combinedArray = combinedArray;
for (CombinedArrayClass *month in combinedCaseCredits){
NSLog(@"Month %@, Data1 %@, Data2 %@, Data3 %@", month.month, month.property1, month.property2, month.property3);
}
1 Answer
![Stephen Hancocks](https://uploads.teamtreehouse.com/production/profile-photos/134900/micro_1233492_10153210736335422_423015326_n.jpg)
Stephen Hancocks
9,271 PointsI've resolved the issue myself.
I moved the line
CombinedArrayClass *objectForCombinedArray = [[CombinedArrayClass alloc]init];
within the loop.
I must have been copying and overwriting the only initialised objectForCombinedArray. Allocating and initialising a new objectForCombinedArray each time has built the array as I wanted.
A beginners mistake, but pleased I've found the answer myself