The code below should be the correct output in the end of chapter 23. I am confused about the last part which is the output when giving up ownership of whole array
2016-01-08 18:10:08.465 BMITime[45849:1407799] Employees: (
"<EmployeeID 0: $0 in assets",
"<EmployeeID 1: $503 in assets",
"<EmployeeID 2: $469 in assets",
"<EmployeeID 3: $768 in assets",
"<EmployeeID 4: $0 in assets",
"<EmployeeID 5: $836 in assets",
"<EmployeeID 6: $819 in assets",
"<EmployeeID 7: $384 in assets",
"<EmployeeID 8: $0 in assets",
"<EmployeeID 9: $486 in assets"
)
2016-01-08 18:10:08.466 BMITime[45849:1407799] Giving up ownership of one employee
2016-01-08 18:10:08.466 BMITime[45849:1407799] deallcating <EmployeeID 5: $836 in assets
2016-01-08 18:10:08.466 BMITime[45849:1407799] allAssets: (
"<Laptop 0: $350,assigned to <EmployeeID 3: $768 in assets",
"<Laptop 1: $367,assigned to <EmployeeID 6: $819 in assets",
"<Laptop 2: $384,assigned to <EmployeeID 7: $384 in assets",
"<Laptop 3: $401 unassigned>",
"<Laptop 4: $418,assigned to <EmployeeID 3: $768 in assets",
"<Laptop 5: $435 unassigned>",
"<Laptop 6: $452,assigned to <EmployeeID 6: $819 in assets",
"<Laptop 7: $469,assigned to <EmployeeID 2: $469 in assets",
"<Laptop 8: $486,assigned to <EmployeeID 9: $486 in assets",
"<Laptop 9: $503,assigned to <EmployeeID 1: $503 in assets"
)
2016-01-08 18:10:08.466 BMITime[45849:1407799] Giving up ownership of arrays
2016-01-08 18:10:08.467 BMITime[45849:1407799] deallocating <Laptop 3: $401 unassigned>
2016-01-08 18:10:08.467 BMITime[45849:1407799] deallocating <Laptop 5: $435 unassigned>
2016-01-08 18:10:08.467 BMITime[45849:1407799] deallcating <EmployeeID 0: $0 in assets
2016-01-08 18:10:08.467 BMITime[45849:1407799] deallcating <EmployeeID 1: $503 in assets
2016-01-08 18:10:08.498 BMITime[45849:1407799] deallocating <Laptop 9: $503 unassigned>
2016-01-08 18:10:08.498 BMITime[45849:1407799] deallcating <EmployeeID 2: $469 in assets
2016-01-08 18:10:08.498 BMITime[45849:1407799] deallocating <Laptop 7: $469 unassigned>
2016-01-08 18:10:08.498 BMITime[45849:1407799] deallcating <EmployeeID 3: $768 in assets
2016-01-08 18:10:08.498 BMITime[45849:1407799] deallocating <Laptop 0: $350 unassigned>
2016-01-08 18:10:08.498 BMITime[45849:1407799] deallocating <Laptop 4: $418 unassigned>
2016-01-08 18:10:08.499 BMITime[45849:1407799] deallcating <EmployeeID 4: $0 in assets
2016-01-08 18:10:08.499 BMITime[45849:1407799] deallcating <EmployeeID 6: $819 in assets
2016-01-08 18:10:08.499 BMITime[45849:1407799] deallocating <Laptop 1: $367 unassigned>
2016-01-08 18:10:08.499 BMITime[45849:1407799] deallocating <Laptop 6: $452 unassigned>
2016-01-08 18:10:08.499 BMITime[45849:1407799] deallcating <EmployeeID 7: $384 in assets
2016-01-08 18:10:08.499 BMITime[45849:1407799] deallocating <Laptop 2: $384 unassigned>
2016-01-08 18:10:08.499 BMITime[45849:1407799] deallcating <EmployeeID 8: $0 in assets
2016-01-08 18:10:08.500 BMITime[45849:1407799] deallcating <EmployeeID 9: $486 in assets
2016-01-08 18:10:08.500 BMITime[45849:1407799] deallocating <Laptop 8: $486 unassigned>
Program ended with exit code: 0
[code]
NSMutableArray *employees = [[NSMutableArray alloc] init];
//Greate an array of employee objects
for (int i = 0; i<10; i++){
BNREmployee *mikey = [[BNREmployee alloc] init];
mikey.weightInKilos = 90+i;
mikey.heightInMeters = 1.8-i/10;
mikey.employeeID = i;
[employees addObject:mikey];
}
NSMutableArray *allAssets = [[NSMutableArray alloc] init];
//create 10 assets
for (int i = 0;i<10;i++){
BNRAsset *asset = [[BNRAsset alloc] init];
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];
[randomEmployee addAssets:asset];
[allAssets addObject:asset];
}
NSLog(@"Employees: %@",employees);
NSLog(@"Giving up ownership of one employee");
[employees removeObjectAtIndex:5];
NSLog(@"allAssets: %@",allAssets);
NSLog(@"Giving up ownership of arrays");
allAssets = nil;
employees = nil;[/code]
Above is the code in main.
But when I delete the code: allAssets = nil; the sequence of the last part in output is different!
2016-01-08 18:17:10.713 BMITime[45859:1411984] Employees: (
"<EmployeeID 0: $0 in assets",
"<EmployeeID 1: $503 in assets",
"<EmployeeID 2: $469 in assets",
"<EmployeeID 3: $768 in assets",
"<EmployeeID 4: $0 in assets",
"<EmployeeID 5: $836 in assets",
"<EmployeeID 6: $819 in assets",
"<EmployeeID 7: $384 in assets",
"<EmployeeID 8: $0 in assets",
"<EmployeeID 9: $486 in assets"
)
2016-01-08 18:17:10.714 BMITime[45859:1411984] Giving up ownership of one employee
2016-01-08 18:17:10.714 BMITime[45859:1411984] deallcating <EmployeeID 5: $836 in assets
2016-01-08 18:17:10.714 BMITime[45859:1411984] allAssets: (
"<Laptop 0: $350,assigned to <EmployeeID 3: $768 in assets",
"<Laptop 1: $367,assigned to <EmployeeID 6: $819 in assets",
"<Laptop 2: $384,assigned to <EmployeeID 7: $384 in assets",
"<Laptop 3: $401 unassigned>",
"<Laptop 4: $418,assigned to <EmployeeID 3: $768 in assets",
"<Laptop 5: $435 unassigned>",
"<Laptop 6: $452,assigned to <EmployeeID 6: $819 in assets",
"<Laptop 7: $469,assigned to <EmployeeID 2: $469 in assets",
"<Laptop 8: $486,assigned to <EmployeeID 9: $486 in assets",
"<Laptop 9: $503,assigned to <EmployeeID 1: $503 in assets"
)
2016-01-08 18:17:10.714 BMITime[45859:1411984] Giving up ownership of arrays
2016-01-08 18:17:10.714 BMITime[45859:1411984] deallcating <EmployeeID 0: $0 in assets
2016-01-08 18:17:10.714 BMITime[45859:1411984] deallcating <EmployeeID 1: $503 in assets
2016-01-08 18:17:10.719 BMITime[45859:1411984] deallcating <EmployeeID 2: $469 in assets
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallcating <EmployeeID 3: $768 in assets
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallcating <EmployeeID 4: $0 in assets
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallcating <EmployeeID 6: $819 in assets
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallcating <EmployeeID 7: $384 in assets
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallcating <EmployeeID 8: $0 in assets
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallcating <EmployeeID 9: $486 in assets
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallocating <Laptop 0: $350 unassigned>
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallocating <Laptop 1: $367 unassigned>
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallocating <Laptop 2: $384 unassigned>
2016-01-08 18:17:10.720 BMITime[45859:1411984] deallocating <Laptop 3: $401 unassigned>
2016-01-08 18:17:10.721 BMITime[45859:1411984] deallocating <Laptop 4: $418 unassigned>
2016-01-08 18:17:10.756 BMITime[45859:1411984] deallocating <Laptop 5: $435 unassigned>
2016-01-08 18:17:10.756 BMITime[45859:1411984] deallocating <Laptop 6: $452 unassigned>
2016-01-08 18:17:10.756 BMITime[45859:1411984] deallocating <Laptop 7: $469 unassigned>
2016-01-08 18:17:10.756 BMITime[45859:1411984] deallocating <Laptop 8: $486 unassigned>
2016-01-08 18:17:10.756 BMITime[45859:1411984] deallocating <Laptop 9: $503 unassigned>
Program ended with exit code: 0
You see that? The assets deallocated after the employee deallocated. I wonder why the sequence is different. By the way, now that “allAssets = nil” has been deleted, the assets still have the owner, which is the *allAssets, why it still deallocated? And if I delete both the code " allAssets = nil and employee = nil", the sequence is the same as first one. I became more confused. Does anyone can help explain? I will be appreciated.