Unusual Output


#1

When I run the BMITime program that we build in Chapter 20 I am getting the following output (before deallocation occurs):

Employees: (
"<Employee 0: $0 in assets, (null)>",
"<Employee 1: $503 in assets, (\n “<Laptop 9: $503>”\n)>",
"<Employee 2: $469 in assets, (\n “<Laptop 7: $469>”\n)>",
"<Employee 3: $768 in assets, (\n “<Laptop 0: $350>”,\n “<Laptop 4: $418>”\n)>",
"<Employee 4: $0 in assets, (null)>",
"<Employee 5: $836 in assets, (\n “<Laptop 3: $401>”,\n “<Laptop 5: $435>”\n)>",
"<Employee 6: $819 in assets, (\n “<Laptop 1: $367>”,\n “<Laptop 6: $452>”\n)>",
"<Employee 7: $384 in assets, (\n “<Laptop 2: $384>”\n)>",
"<Employee 8: $0 in assets, (null)>",
"<Employee 9: $486 in assets, (\n “<Laptop 8: $486>”\n)>"
)

I can’t figure out why I am getting all the \n characters. I notice that in the book the output does not include the “Laptop” labels, although my understanding of the code seems to indicate that it should be there.

Any help would be much appreciated!


#2

Here is my code, if that helps.

BNRAsset.h

[code]#import <Foundation/Foundation.h>

@interface BNRAsset : NSObject

@property (nonatomic, copy) NSString *label;
@property (nonatomic) unsigned int resaleValue;

@end[/code]

BNRAsset.m

[code]#import “BNRAsset.h”

@implementation BNRAsset

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<%@: $%u>", self.label, self.resaleValue];
    }

  • (void)dealloc
    {
    NSLog(@“deallocating %@”, self);
    }

@end[/code]

BNREmployee.h

[code]#import “BNRPerson.h”
@class BNRAsset;

@interface BNREmployee : BNRPerson

@property (nonatomic) unsigned int employeeID;
@property (nonatomic) NSDate *hireDate;
@property (nonatomic, copy) NSArray *assets;

  • (double)yearsOfEmployment;
  • (void)addAsset:(BNRAsset *)a;
  • (void)removeAsset:(BNRAsset *)r;
  • (unsigned int)valueOfAssets;

@end[/code]

BNREmployee.m

[code]#import “BNREmployee.h”
#import “BNRAsset.h”

// A class extension
@interface BNREmployee()

{
NSMutableArray *_assets;
}

@property (nonatomic) unsigned int officeAlarmCode;

@end

@implementation BNREmployee

// Accessors for assets properties

  • (void)setAssets:(NSArray *)a
    {
    _assets = [a mutableCopy];
    }

  • (NSArray *)assets
    {
    return [_assets copy];
    }

  • (void)addAsset:(BNRAsset *)a
    {
    // Is assets nil?
    if (!_assets) {

      // Create the array
      _assets = [[NSMutableArray alloc] init];
    

    }
    [_assets addObject:a];
    }

  • (void)removeAsset:(BNRAsset *)r
    {
    // Is assets nil?
    if (_assets) {
    [_assets removeObject:r];
    }
    }

  • (unsigned int)valueOfAssets
    {
    // Sum up the resale value of the assets
    unsigned int sum = 0;
    for (BNRAsset *a in _assets) {
    sum += [a resaleValue];
    }
    return sum;
    }

  • (double)yearsOfEmployment
    {
    // Do I have a non-nil hireDate?
    if (self.hireDate) {
    // NSTimeInterval is the same as double
    NSDate *now = [NSDate date];
    NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate];
    return secs / 31557600.0; // Seconds per year
    } else {
    return 0;
    }
    }

  • (float)bodyMassIndex
    {
    float normalBMI = [super bodyMassIndex];
    return normalBMI * 0.9;
    }

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<Employee %u: $%u in assets, %@>", self.employeeID, self.valueOfAssets, self.assets];
    }

  • (void)dealloc
    {
    NSLog(@“deallocating %@”, self);
    }

@end[/code]

main.m

[code]#import <Foundation/Foundation.h>
#import “BNREmployee.h”
#import “BNRAsset.h”

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

@autoreleasepool {
    
    // Create an array of BNREmployee objects
    NSMutableArray *employees = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++) {
        // Create an instance of BNREmployee
        BNREmployee *mikey = [[BNREmployee alloc] init];
        
        // Give the instance variables interesting values
        mikey.weightInKilos = 90 + i;
        mikey.heightInMeters = 1.8 - i/10.0;
        mikey.employeeID = i;
        
        // Put the employee in the employees array
        [employees addObject:mikey];
    }
    
    // Create 10 assets
    for (int i = 0; i < 10; i++) {
        // Create an asset
        BNRAsset *asset = [[BNRAsset alloc] init];
        
        // Give it an interesting label
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
        asset.label = currentLabel;
        asset.resaleValue = 350 + i * 17;
        
        // Get a random number between 0 and 9 inclusive
        NSUInteger randomIndex = random() % [employees count];
        
        // Find that employee
        BNREmployee *randomEmployee = [employees objectAtIndex:randomIndex];
        
        // Assign the asset to the employee
        [randomEmployee addAsset:asset];
    }
    
    NSLog(@"Employees: %@", employees);
    
    NSLog(@"Giving up ownership of one employee");
    
    [employees removeObjectAtIndex:5];
    
    NSLog(@"Giving up ownership of arrays");
    
    employees = nil;
    
    NSLog(@"Removing all assets");
    
    for (BNREmployee *e in employees) {
        for (BNRAsset *a in e.assets) {
            [e removeAsset:a];
        }
    }
    
}
return 0;

}[/code]