Gold Questions Chapter 2 --- strange output


#1

I’m hoping someone can answer my curious questions regarding the following code I created below. When calling the description method for a a container object, everything outputs nicely until I hit a container within my container. I have a weird output of:

[quote]Total worth = $243, With these items in it: (
“Rusty Spork (3K6Q7): Worth $73, recorded on 2012-05-24 23:21:28 +0000”,
“Shiny Bear (5F3J5): Worth $78, recorded on 2012-05-24 23:21:28 +0000”,
“Container Two: Total worth = $276, With these items in it: (\n “Fluffy Mac (6Q2V9): Worth $92, recorded on 2012-05-24 23:21:28 +0000”\n)”
)
[/quote]

Notice when it lists Container Two it for 1.) has random nils \n … maybe for ending an array in an array? and for 2.) my item’s worth adds up very very strangely. I was wondering if there’s a nice way to get rid of the nils and possibly the incorrect adding. I’m afraid that I may have to check and compare the classes of the object in my valueInDollars code in order to have that corrected. Either or, this is nice boarder land code for new comers!

BNRContainer.h

[code]//
// BNRContainer.h
// RandomPossessions
//
// Created by Brandon Levasseur on 5/24/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”

@interface BNRContainer : BNRItem{
NSString *containerName;
NSMutableArray *containerArray;
}

-(id)initWithContainerName:(NSString *)name;

-(void)setContainerArray:(id)item;
-(NSMutableArray *)containerArray;

-(void)setContainerName:(NSString *)name;
-(NSString *)containerName;

@end
[/code]

BNRContainer.m

[code]//
// BNRContainer.m
// RandomPossessions
//
// Created by Brandon Levasseur on 5/24/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “BNRContainer.h”

@implementation BNRContainer

-(id)initWithContainerName:(NSString *)name{
self = [super init];

if(self){ // Did the superclass’s designated initializer succeed?
//Give the instance variables initial values
[self setContainerName:name];
containerArray = [[NSMutableArray alloc] init];
}

return self;

}

-(void)setContainerArray:(id)item{
[containerArray addObject:item];
}
-(NSMutableArray *)containerArray{
return containerArray;
}

-(void)setContainerName:(NSString *)name{
containerName = name;
}
-(NSString *)containerName{

return containerName;
}

-(int)valueInDollars{

for(BNRItem *item in containerArray){
    valueInDollars += [item valueInDollars];
    NSLog(@"VALUE: %d", [item valueInDollars]);
}
return valueInDollars;

}

-(NSString *)description{ //overrides the default description method of BN
int arrayCount = [containerArray count];

NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@: Total worth = $%d, With these items in it: %@", containerName,[self valueInDollars], ((arrayCount > 0)? containerArray : @"NULL" )];
return descriptionString;

}

@end
[/code]


#2

\n is an escaped newline character, not a nil. The double quotes in container two are also escaped, eg. "Fluffy … instead of just "Fluffy … (and I’m not sure, why).

Reset valueInDollars before calculating the sum:

-(int)valueInDollars{ valueInDollars = 0; for(BNRItem *item in containerArray){ ...