Gold Challenge brings up question


While solving the Gold Challenge, I was having trouble with the description method of the container. I found out what I was doing wrong, and just want to make sure that I understand this correctly. In the description method of BNRContainer, I was declaring an NSString at the beginning to log the description of the BNRContainer itself:

-(NSString *)description { NSString *containerDescription = [NSString stringWithFormat:@"description..."];

I then looped through each item in the subItem array. I used the stringByAppendingString method, but I did it like this:

for (id s in subItems) { NSString *subItemDescription = [containerDescription stringByAppendingString:[s description]]; }

The string was never appended, and therefore, was never returned. To fix it, I did this:

for (id s in subItems) { containerDescription = [containerDescription stringByAppendingString:[s description]]; }

Is the reason my first solution didn’t work because all pointers declared within the loop only exists locally and will be destroyed once the loop is complete?


Yup, you got it right! A pointer declared within the loop will only excites while the loop excites!


Thank you very much!