Gold Challenge: Formatting Output


Hey folks,

I’m pretty much done with the Gold Challenge but the majority of my time on the gold challenge has been trying to format the output. I’m still not getting it the way i want/expect it. Basically I want the output to look nested. I’ve tried simply adding the array directly into the descriptionString but I see the same problem I’ve seen a couple other folks complain about where the second array gets formatted funky. So I tried the following and the output has me confused because I’m seeing the second array’s subitems twice but at different levels of indentation. Maybe you guys can spot something I’m missing.


//  BNRContainer.h
//  RandomPossessions

#import "BNRItem.h"

@interface BNRContainer : BNRItem
    NSMutableArray *items;

@property NSMutableArray *items;

- (void)updateValue;
- (void)randomObject;
- (void)addObject:(id)obj;



//  BNRContainer.m
//  RandomPossessions

#import "BNRContainer.h"

@implementation BNRContainer

@synthesize items;

- (id)init {
    // create our BNRContainer
    self = [super initWithItemName:[NSString stringWithFormat:@"%@ CONTAINER",[BNRContainer randomName]] valueInDollars:0 serialNumber:[BNRContainer randomSerialNumber]];
    if (self){
        items = [[NSMutableArray alloc] init];
    return self;

- (NSString *)description {			
    // Make sure the total value is updated
    [self updateValue];
    // describe self
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@ (%@): total value $%d, recorded on %@. ", itemName, serialNumber, valueInDollars, dateCreated];
    // Append items to description
    for (BNRContainer *obj in self.items) {
        if ([obj isMemberOfClass:[BNRContainer class]]){
            descriptionString = [descriptionString stringByAppendingFormat:@"\n ----> %@", [obj description]];
            for (BNRContainer *subObj in [obj items]) {
                descriptionString = [descriptionString stringByAppendingFormat:@"\n ------> %@", [subObj description]];
        } else {
           descriptionString = [descriptionString stringByAppendingFormat:@"\n --> %@", [obj description]];
    // Return Description
    return descriptionString;

- (void)updateValue {
    // var to hold the total value of our container
    int totalValue = 0;
    // loop over array to total values
    for (BNRContainer *item in items) {
        totalValue += [item valueInDollars];
    [self setValueInDollars: totalValue];

- (void)randomObject {
    // create a random BNRItem object
    BNRItem *obj = [BNRItem randomItem];
    // add the object to our items container
    [self.items addObject:obj];

- (void)addObject:(id)obj {
    // add the object to our items array
    [self.items addObject:obj];



//  main.m
//  RandomPossessions

#import <Foundation/Foundation.h>
#import "BNRContainer.h"

int main(int argc, const char * argv[])

    @autoreleasepool {
        // Create BNRContainer
        BNRContainer *container1 = [[BNRContainer alloc] init];
        // Generate x random objects
        for (int i = 0; i < 10; i++) {
            [container1 randomObject];
        // add another BNRContainer to our already existing bnr container...
        BNRContainer *container2 = [[BNRContainer alloc] init];
        for (int i = 0; i < 5; i++) {
            [container2 randomObject];
        [container1 addObject:container2];

        // show me the money
        NSLog(@"\n\n %@", container1);

        container2 = nil;
        container1 = nil;
    return 0;


The whole output above is what i see in the console. There should be 10 items and 1 container in “Rusty Sport CONTAINER” and 5 items in “Shiny Bear CONTAINER”. The green I expected. The red I didn’t expect. Any ideas as to why that is happening? I’m sure I’m doing something wrong but I’m not sure what.


When you call

You are actually running the description method again, but this time u are sending it from the inner Container, so all objects inside it are going to be BNRItems. Your if statement is going to return false and this code is going to be executed