Working on NSMutableDictionary


#1

I wrote the code for this part, and I see the CEO = “…” and the CFO = “…”, but I don’t get their dellocation:

#import <Foundation/Foundation.h>
#import "Employee.h"
#import "Asset.h"

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

    @autoreleasepool {
        
        // Create an array of Employees objects
        NSMutableArray *employees = [[NSMutableArray alloc] init];
        
        // Create a diccionary of executives
        NSMutableDictionary *executuves = [[NSMutableDictionary alloc] init];
        
        
        
        for (int i = 0; i < 10 ; i++) {
            
            // Create an instace of Employee
            Employee *person = [[Employee alloc] init];
            
            // Give the instance variables interesting values
            [person setWeightInKilos:90 + i];
            [person setHeightInMeters:1.8 - i/10.0];
            [person setEmployeeID:i];
            
            // Put the employee in the employees array
            [employees addObject:person];      
        
            // Is this the first employee?
            if ( i == 0) {
                [executuves setObject:person forKey:@"CEO"];
            }
            
            // Is this the seccond person?
            if (i == 1) {
                [executuves setObject:person forKey:@"CFO"];
            }
            
        }
        
        NSMutableArray *allAssets = [[NSMutableArray alloc] init];
        
        // Create 10 assets
        for (int i = 0; i < 10; i++) {
            
            // Create an asset
            Asset *asset = [[Asset alloc] init];
            
            // Give it an interesting 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];
            
            // Assing the asset to the employee
            [randomEmployee addAssetsObject:asset];
            
            [allAssets addObject:asset];
        }
        
        NSSortDescriptor *voa = [NSSortDescriptor sortDescriptorWithKey:@"valueOfAssets"
                                                              ascending:YES];
        NSSortDescriptor *ei = [NSSortDescriptor sortDescriptorWithKey:@"employeeID"
                                                             ascending:YES];
        [employees sortUsingDescriptors:[NSArray arrayWithObjects: voa, ei, nil]];
                                 
        
        NSLog(@"Employees: %@", employees);
        
        NSLog(@"Giving up ownership of one employee");
        
        [employees removeObjectAtIndex:5];
        
        NSLog(@"allAssets %@", allAssets);
        
        NSLog(@"executives: %@", executuves);
        executuves = nil;
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];
        NSArray *toBeReclaimed = [allAssets filteredArrayUsingPredicate:predicate];
        NSLog(@"toBeReclaimed: %@", toBeReclaimed);
        toBeReclaimed = nil;
        
        NSLog(@"Giving up ownershipo of array");
        
        allAssets = nil;
        employees = nil;
        
    
    }
    return 0;
}

…this is my result:

2012-06-15 17:01:15.253 BMITime[1429:403] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 7: $34 in assets>",
"<Employee 3: $68 in assets>",
"<Employee 2: $119 in assets>",
"<Employee 6: $119 in assets>",
"<Employee 5: $136 in assets>",
"<Employee 9: $136 in assets>",
"<Employee 1: $153 in assets>"
)
2012-06-15 17:01:15.256 BMITime[1429:403] Giving up ownership of one employee
2012-06-15 17:01:15.256 BMITime[1429:403] deallocating <Employee 2: $119 in assets>
2012-06-15 17:01:15.257 BMITime[1429:403] 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, assigned to <Employee 5: $136 in assets> >",
"<Laptop 4: $68, assigned to <Employee 3: $68 in assets> >",
"<Laptop 5: $85, assigned to <Employee 5: $136 in assets> >",
"<Laptop 6: $102, assigned to <Employee 6: $119 in assets> >",
"<Laptop 7: $119 unassigned>",
"<Laptop 8: $136, assigned to <Employee 9: $136 in assets> >",
"<Laptop 9: $153, assigned to <Employee 1: $153 in assets> >"
)
2012-06-15 17:01:15.257 BMITime[1429:403] executives: {
CEO = “<Employee 0: $0 in assets>”;
CFO = “<Employee 1: $153 in assets>”;
}
2012-06-15 17:01:15.258 BMITime[1429:403] toBeReclaimed: (
"<Laptop 1: $17, assigned to <Employee 6: $119 in assets> >",
"<Laptop 3: $51, assigned to <Employee 5: $136 in assets> >",
"<Laptop 5: $85, assigned to <Employee 5: $136 in assets> >",
"<Laptop 6: $102, assigned to <Employee 6: $119 in assets> >",
"<Laptop 8: $136, assigned to <Employee 9: $136 in assets> >",
"<Laptop 9: $153, assigned to <Employee 1: $153 in assets> >"
)
2012-06-15 17:01:15.258 BMITime[1429:403] Giving up ownershipo of array
2012-06-15 17:01:15.259 BMITime[1429:403] deallocation <Laptop 7: $119 unassigned>
2012-06-15 17:01:15.259 BMITime[1429:403] deallocating <Employee 0: $0 in assets>
2012-06-15 17:01:15.259 BMITime[1429:403] deallocating <Employee 4: $0 in assets>
2012-06-15 17:01:15.260 BMITime[1429:403] deallocating <Employee 8: $0 in assets>
2012-06-15 17:01:15.260 BMITime[1429:403] deallocating <Employee 1: $153 in assets>
2012-06-15 17:01:15.261 BMITime[1429:403] deallocation <Laptop 9: $153 unassigned>
2012-06-15 17:01:15.261 BMITime[1429:403] deallocating <Employee 9: $136 in assets>
2012-06-15 17:01:15.261 BMITime[1429:403] deallocation <Laptop 8: $136 unassigned>
2012-06-15 17:01:15.262 BMITime[1429:403] deallocating <Employee 5: $136 in assets>
2012-06-15 17:01:15.262 BMITime[1429:403] deallocation <Laptop 3: $51 unassigned>
2012-06-15 17:01:15.262 BMITime[1429:403] deallocation <Laptop 5: $85 unassigned>
2012-06-15 17:01:15.263 BMITime[1429:403] deallocating <Employee 7: $34 in assets>
2012-06-15 17:01:15.263 BMITime[1429:403] deallocation <Laptop 2: $34 unassigned>
2012-06-15 17:01:15.263 BMITime[1429:403] deallocating <Employee 6: $119 in assets>
2012-06-15 17:01:15.286 BMITime[1429:403] deallocation <Laptop 1: $17 unassigned>
2012-06-15 17:01:15.287 BMITime[1429:403] deallocation <Laptop 6: $102 unassigned>
2012-06-15 17:01:15.287 BMITime[1429:403] deallocating <Employee 3: $68 in assets>
2012-06-15 17:01:15.287 BMITime[1429:403] deallocation <Laptop 0: $0 unassigned>
2012-06-15 17:01:15.288 BMITime[1429:403] deallocation <Laptop 4: $68 unassigned>

any clues??

Gracias!!!


#2

It looks to me like they’re deallocating just fine:


#3

upss!!..thats right!!