Non-nil holder condition


#1

[code]#import “Asset.h”
#import “Employee.h”

@implementation Asset

@synthesize label, resaleValue, holder;

  • (NSString *)description;
    {
    //Is holder non-nil?
    if ([self holder]) {
    return [NSString stringWithFormat:@"<%@: %d, assigned to %@>", [self label], [self resaleValue], [self holder]]; }else{ return [NSString stringWithFormat:@"<%@: %d unassigned>", [self label], [self resaleValue]];
    }

}

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

When executing the program, I noticed in Asset.m above, that [self holder] is apparently never non-nil. To test this, I set a breakpoint at the first of the return statements and verified that that portion of the if statement’s logic never gets executed which leads me to question whether the statement is necessary, and if so, why? Would it not have been more economical to code this as:

if (![self holder]) {
    return [NSString stringWithFormat:@"<%@: $%d unassigned>", [self label], [self resaleValue]];
}

#2

If you look at the output of the program, which should be almost exactly what is on page #131 (1st ed.) then you can see that both of these conditions are hit at one time or another.

My output (abbreviated):

2013-12-22 12:29:04.337 BMITime[1368:303] Employees: ( "<Employee 0: $0 in assets>", ... "<Employee 9: $136 in assets>" ) 2013-12-22 12:29:04.339 BMITime[1368:303] Giving up ownership of one employee 2013-12-22 12:29:04.339 BMITime[1368:303] deallocating <Employee 5: $136 in assets> 2013-12-22 12:29:04.340 BMITime[1368:303] allAssets: ( "<Laptop 0: $0, assigned to <Employee 3: $68 in assets>>", "<Laptop 1: $17, assigned to <Employee 6: $119 in assets>>", "<Laptop 2: $34, assigned to <Employee 7: $34 in assets>>", "<Laptop 3: $51, unassigned>", "<Laptop 4: $68, assigned to <Employee 3: $68 in assets>>", "<Laptop 5: $85, unassigned>", "<Laptop 6: $102, assigned to <Employee 6: $119 in assets>>", "<Laptop 7: $119, assigned to <Employee 2: $119 in assets>>", "<Laptop 8: $136, assigned to <Employee 9: $136 in assets>>", "<Laptop 9: $153, assigned to <Employee 1: $153 in assets>>" ) 2013-12-22 12:29:04.340 BMITime[1368:303] Giving up ownership of array 2013-12-22 12:29:04.341 BMITime[1368:303] deallocating <Laptop 3: $51, unassigned> 2013-12-22 12:29:04.341 BMITime[1368:303] deallocating <Laptop 5: $85, unassigned> 2013-12-22 12:29:04.342 BMITime[1368:303] deallocating <Employee 0: $0 in assets> 2013-12-22 12:29:04.343 BMITime[1368:303] deallocating <Employee 1: $153 in assets> 2013-12-22 12:29:04.343 BMITime[1368:303] deallocating <Laptop 9: $153, unassigned> 2013-12-22 12:29:04.344 BMITime[1368:303] deallocating <Employee 2: $119 in assets> 2013-12-22 12:29:04.344 BMITime[1368:303] deallocating <Laptop 7: $119, unassigned> 2013-12-22 12:29:04.345 BMITime[1368:303] deallocating <Employee 3: $68 in assets> 2013-12-22 12:29:04.345 BMITime[1368:303] deallocating <Laptop 0: $0, unassigned> 2013-12-22 12:29:04.346 BMITime[1368:303] deallocating <Laptop 4: $68, unassigned> 2013-12-22 12:29:04.346 BMITime[1368:303] deallocating <Employee 4: $0 in assets> 2013-12-22 12:29:04.346 BMITime[1368:303] deallocating <Employee 6: $119 in assets> 2013-12-22 12:29:04.347 BMITime[1368:303] deallocating <Laptop 1: $17, unassigned> 2013-12-22 12:29:04.347 BMITime[1368:303] deallocating <Laptop 6: $102, unassigned> 2013-12-22 12:29:04.348 BMITime[1368:303] deallocating <Employee 7: $34 in assets> 2013-12-22 12:29:04.348 BMITime[1368:303] deallocating <Laptop 2: $34, unassigned> 2013-12-22 12:29:04.349 BMITime[1368:303] deallocating <Employee 8: $0 in assets> 2013-12-22 12:29:04.349 BMITime[1368:303] deallocating <Employee 9: $136 in assets> 2013-12-22 12:29:04.349 BMITime[1368:303] deallocating <Laptop 8: $136, unassigned>

This line shows the first condition being hit: "<Laptop 0: $0, assigned to <Employee 3: $68 in assets>>"
This line shows the second condition being hit: “<Laptop 3: $51, unassigned>”

The holder for assets “Laptop 3, Laptop 5” (in this run of the program) are nil because employee 5 was removed and previously held those objects. The rest are not nil because they are currently held by an employee.

Hope that helps!