Is this code redundant in the book?


#1

In the collection class chapter, in BNREmployee.h there is thing code

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

@interface BNREmployee : BNRPerson
{
NSMutableArray *_assets;
}

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

  • (double)yearsOfEmployment;
  • (void)addAsset:(BNRAsset *)a;
  • (unsigned int)valueOfAssets;[/code]

in BNREmployee.m

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

@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];
    }

  • (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
    {

    [/code]

in BNREmployee.m there is this code

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

Ok when i commented out this code, the whole program still worked fine. I was wondering the use of this code here.
Here is the 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;

}
return 0;

}
[/code]