Employees and Assets part


#1

Hello i am reading about object ownership, and i don’t understand [quote]2014-12-20 12:23:04.522 MyFirstClass[11804:303] Giving up ownership of one employee
2014-12-20 12:23:04.523 MyFirstClass[11804:303] deallocation <Employee 5: $836 in assets>
2014-12-20 12:23:04.524 MyFirstClass[11804:303] deallocating <Laptop 3: 401>
2014-12-20 12:23:04.525 MyFirstClass[11804:303] deallocating <Laptop 5: 435>[/quote] when you give up ownership of 1 employee, TWO assets are deallocated? Why is it displaying that it is deallocating two assets.

Is it because when generating a random number for index it can get same number/index twice? So some “Employees” may have assets and some don’t?

Output

[quote]2014-12-20 12:23:04.514 MyFirstClass[11804:303] value of asset is: 350
2014-12-20 12:23:04.517 MyFirstClass[11804:303] value of asset is: 367
2014-12-20 12:23:04.517 MyFirstClass[11804:303] value of asset is: 384
2014-12-20 12:23:04.518 MyFirstClass[11804:303] value of asset is: 401
2014-12-20 12:23:04.518 MyFirstClass[11804:303] value of asset is: 768
2014-12-20 12:23:04.519 MyFirstClass[11804:303] value of asset is: 836
2014-12-20 12:23:04.519 MyFirstClass[11804:303] value of asset is: 819
2014-12-20 12:23:04.520 MyFirstClass[11804:303] value of asset is: 469
2014-12-20 12:23:04.520 MyFirstClass[11804:303] value of asset is: 486
2014-12-20 12:23:04.520 MyFirstClass[11804:303] value of asset is: 503
2014-12-20 12:23:04.521 MyFirstClass[11804:303] Employees (
"<Employee 0: $0 in assets>",
"<Employee 1: $503 in assets>",
"<Employee 2: $469 in assets>",
"<Employee 3: $768 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 5: $836 in assets>",
"<Employee 6: $819 in assets>",
"<Employee 7: $384 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 9: $486 in assets>"
)

2014-12-20 12:23:04.522 MyFirstClass[11804:303] Giving up ownership of one employee
2014-12-20 12:23:04.523 MyFirstClass[11804:303] deallocation <Employee 5: $836 in assets>
2014-12-20 12:23:04.524 MyFirstClass[11804:303] deallocating <Laptop 3: 401>
2014-12-20 12:23:04.525 MyFirstClass[11804:303] deallocating <Laptop 5: 435>
2014-12-20 12:23:04.525 MyFirstClass[11804:303] Giving up ownership of arrays
2014-12-20 12:23:04.526 MyFirstClass[11804:303] deallocation <Employee 0: $0 in assets>
2014-12-20 12:23:04.526 MyFirstClass[11804:303] deallocation <Employee 1: $503 in assets>
2014-12-20 12:23:04.527 MyFirstClass[11804:303] deallocating <Laptop 9: 503>
2014-12-20 12:23:04.527 MyFirstClass[11804:303] deallocation <Employee 2: $469 in assets>
2014-12-20 12:23:04.527 MyFirstClass[11804:303] deallocating <Laptop 7: 469>
2014-12-20 12:23:04.528 MyFirstClass[11804:303] deallocation <Employee 3: $768 in assets>
2014-12-20 12:23:04.528 MyFirstClass[11804:303] deallocating <Laptop 0: 350>
2014-12-20 12:23:04.529 MyFirstClass[11804:303] deallocating <Laptop 4: 418>
2014-12-20 12:23:04.529 MyFirstClass[11804:303] deallocation <Employee 4: $0 in assets>
2014-12-20 12:23:04.530 MyFirstClass[11804:303] deallocation <Employee 6: $819 in assets>
2014-12-20 12:23:04.530 MyFirstClass[11804:303] deallocating <Laptop 1: 367>
2014-12-20 12:23:04.531 MyFirstClass[11804:303] deallocating <Laptop 6: 452>
2014-12-20 12:23:04.532 MyFirstClass[11804:303] deallocation <Employee 7: $384 in assets>
2014-12-20 12:23:04.532 MyFirstClass[11804:303] deallocating <Laptop 2: 384>
2014-12-20 12:23:04.533 MyFirstClass[11804:303] deallocation <Employee 8: $0 in assets>
2014-12-20 12:23:04.533 MyFirstClass[11804:303] deallocation <Employee 9: $486 in assets>
2014-12-20 12:23:04.534 MyFirstClass[11804:303] deallocating <Laptop 8: 486>
Program ended with exit code: 0[/quote]

Main

[code]//
// main.m
// MyFirstClass
//
// Created by Jacob Rude Hansen on 15/12/14.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “Employee.h”
#import "Asset.h"
int main(int argc, const char * argv[])
{

@autoreleasepool {
    // create an array of Employee object.
    NSMutableArray *employees = [[NSMutableArray alloc] init];
    
    
    for(int i = 0; i < 10; i++){
        // Create an instance of Employee.
        Employee *mikey = [[Employee alloc] init];
        
        
        // Give the instance interesting values.
        mikey.weightInKilos = 90 + i;
        mikey.heightInMeters = 1.8 - i/10.0;
        mikey.employeeID = i;
        
        // Put the employees in the employee array.
        [employees addObject:mikey];
    }
    
    // Create 10 assets.
    for(int i = 0; i < 10; i++){
        // Create an asset object.
        Asset *asset = [[Asset 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.
        // Select random employee of employees array.
        Employee *randomEmployee = [employees objectAtIndex:randomIndex];
        
        // Assign the asset to the employee.
        [randomEmployee addAsset:asset];
        
        NSLog(@"value of asset is: %u", randomEmployee.valueOfAssets);
        
    }
    
    // Display all employees array.
    NSLog(@"Employees %@", employees);
    
    
    NSLog(@"Giving up ownership of one employee");
    
    [employees removeObjectAtIndex:5];
    
    
    NSLog(@"Giving up ownership of arrays");
    
    employees = nil;
    
    

    
}
return 0;

}

[/code]

Employee.h

[code]//
// Employee.h
// MyFirstClass
//
// Created by Jacob Rude Hansen on 16/12/14.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset;

@interface Employee : Person {
NSMutableArray *_assets;

}

@property (nonatomic) unsigned int employeeID;
@property (nonatomic) unsigned int officeAlarmCode;

@property (nonatomic) NSDate *hireDate;

// Object ownership (Assets).
@property (nonatomic) NSArray *assets;

-(void) addAsset: (Asset *) a;
-(unsigned int) valueOfAssets;

  • (double) yearsOfEmployment;
    @end
    [/code]

Employee.m

[code]//
// Employee.m
// MyFirstClass
//
// Created by Jacob Rude Hansen on 16/12/14.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//

#import “Employee.h”
#import “Asset.h”

@implementation Employee

-(double) yearsOfEmployment{
if(self.hireDate){
NSDate *now = [NSDate date];
NSTimeInterval seconds = [now timeIntervalSinceDate:self.hireDate];
return seconds / 31557600.0;
}
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];
}

-(void) dealloc{
NSLog(@“deallocation %@”, self); // Display the object when it is deallocated.
}

// Object ownership (Assets).
-(void) setAssets:(NSArray *)a{
_assets = [a mutableCopy];
}

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

// Add asset to _assets mutable array.
-(void) addAsset:(Asset *)a{
if(!_assets){
// Create the array.
_assets = [[NSMutableArray alloc] init];
}
// add asset object to _assets array.
[_assets addObject:a];

}

// Getter for tbe value of asset.
-(unsigned int) valueOfAssets{
unsigned int sum = 0;
for(Asset *a in _assets){
sum += [a resaleValue];
}
return sum;
}

@end
[/code]

Asset.h

[code]//
// Asset.h
// MyFirstClass
//
// Created by Jacob Rude Hansen on 19/12/14.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Asset : NSObject

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

@end
[/code]

Asset.m

[code]//
// Asset.m
// MyFirstClass
//
// Created by Jacob Rude Hansen on 19/12/14.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//

#import “Asset.h”

@implementation Asset

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

-(void) dealloc{
NSLog(@“deallocating %@: %u”, self, resaleValue);{// e.g : deallocating <Laptop 3: 455>
}
@end
[/code]