All employees are "0" in the NSMutableArray


#1

My output from the example exercise list all the elements with index “0” (I think I said that right). Why would NSLog treat this array differently?

2011-11-23 23:10:44.510 BMITime[3304:707] employee count is: 10
2011-11-23 23:10:44.512 BMITime[3304:707] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 0: $153 in assets>",
"<Employee 0: $119 in assets>",
"<Employee 0: $68 in assets>",
"<Employee 0: $0 in assets>",
"<Employee 0: $136 in assets>",
"<Employee 0: $119 in assets>",
"<Employee 0: $34 in assets>",
"<Employee 0: $0 in assets>",
"<Employee 0: $136 in assets>"
)
2011-11-23 23:10:44.512 BMITime[3304:707] Giving up ownership of one employee
2011-11-23 23:10:44.512 BMITime[3304:707] deallocating <Employee 0: $136 in assets>
2011-11-23 23:10:44.513 BMITime[3304:707] deallocating <Laptop 3: $51 >
2011-11-23 23:10:44.513 BMITime[3304:707] deallocating <Laptop 5: $85 >
2011-11-23 23:10:44.514 BMITime[3304:707] Giving up ownership of array[size=85]
2011-11-23 23:10:44.514 BMITime[3304:707] deallocating <Employee 0: $0 in assets>
…snip

[code]//
// main.m
// BMITime
//

#import <Foundation/Foundation.h>
#import “Employee.h”
#import “Asset.h”

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

@autoreleasepool {
    
    //Create an array of Employee objects
    NSMutableArray *employees = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++){
        
        // Create an instance of Employee 
        Employee *person = [[Employee alloc] init];
        
        // Give the instance variables intresting values
        [person setWeightInKilos:90 + i];
        [person setHeightInMeters:1.8 - i/10.0];
        [person setHeightInMeters:i];
        
        // Put the employee in the employees array
        [employees addObject:person];
    }
    
    // Create 10 assests
    for (int i = 0; i < 10; i++) {
        // Create an asset
        Asset *asset = [[Asset alloc] init];
        
        // Give it a intresting label
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
        [asset setLabel:currentLabel];
        [asset setResaleValue:i * 17];
        
        // Get a random number between 0 and 9 inclusive
        NSUInteger randomIndex = random() % [ employees count];
        
        // Find that employee
        Employee *randomEmployee = [ employees objectAtIndex:randomIndex];
        
        // Assign that asset to the employee
        [randomEmployee addAssetsObject:asset];
    }
    
    
    NSLog(@"employee count is: %lu", [employees count]);                                
    
    NSLog(@"Employees: %@", employees);
    
    NSLog(@"Giving up ownership of one employee");
    
    [employees removeObjectAtIndex:5];
    
    NSLog(@"Giving up ownership of array");
    
    employees = nil;
}
return 0;

}[/code]


#2

You called setHeightInMeters: twice. It should be:

[person setHeightInMeters:1.8 - i/10.0]; [person setEmployeeID:i];


#3

Ah yes… thanks!