Can Someone Pls Help With My Code?


#1

When I run the chapter example, it DOESN’T output any of the deallocation descriptions? I’ve searched my code several times, over and over, and cannot find the problem. I must be missing something. Any help or assistance appreciated? Thanks.

Here is a sample of the program execution:

2012-09-17 18:22:49.013 BMITime[405:403] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 1: $153 in assets>",
"<Employee 2: $119 in assets>",
"<Employee 3: $68 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 5: $136 in assets>",
"<Employee 6: $119 in assets>",
"<Employee 7: $34 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 9: $136 in assets>"
)
2012-09-17 18:22:49.014 BMITime[405:403] Giving up ownership of one employee
2012-09-17 18:22:49.015 BMITime[405:403] Giving up ownership of array

Here are my files:


Employee.h

#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset;
@interface Employee : Person
{
int employeeID;
NSMutableArray *assets;
}
@property int employeeID;

  • (void) addAssetsObject:(Asset *)a;
  • (unsigned int)valueOfAssets;
    @end

Employee.m

#import “Employee.h”
#import “Asset.h”
@implementation Employee
@synthesize employeeID;

  • (void) addAssetsObject:(Asset *)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 (Asset *a in assets)
    {
    sum += [a resaleValue];
    }
    return sum;
    }

  • (float) bodyMassIndex
    {
    float normalBMI = [super bodyMassIndex];
    return normalBMI * 0.9;
    }

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<Employee %d: $%d in assets>", [self employeeID], [self valueOfAssets]];
    }

  • (void) dealloc
    {
    NSLog(@“deallocating %@”, self);
    }
    @end


Asset.h

#import <Foundation/Foundation.h>
@interface Asset : NSObject
{
NSString *label;
unsigned int resaleValue;
}
@property (strong) NSString *label;
@property unsigned int resaleValue;
@end


Asset.m

#import “Asset.h”
@implementation Asset
@synthesize label, resaleValue;

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<%@: $%d >", [self label], [self resaleValue]];
    }
  • (void) dealloc
    {
    NSLog(@“deallocating %@”, self);
    }
    @end

main.m

#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 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];
        
    }
    
    // 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];
        
        // Assign that asset to that employee
        [randomEmployee addAssetsObject:asset];
        
    }
    
    NSLog(@"Employees: %@", employees);
    
    NSLog(@"Giving up ownership of one employee");
    
    [employees removeObjectAtIndex:5];
     
    NSLog(@"Giving up ownership of array");
    
    employees = nil;       
}
return 0;

}


#2

Please post all your files again, by using the Code tag.

If you do that, they will look like this:

// Employee.h

#import <Foundation/Foundation.h>
#import "Person.h"

@class Asset;
@interface Employee : Person
{
    int employeeID;
    NSMutableArray *assets;
}
@property int employeeID;
- (void) addAssetsObject:(Asset *)a;
- (unsigned int)valueOfAssets;
@end

This will make it easier to view your code.


#3

When I run the chapter example, it DOESN’T output any of the deallocation descriptions? I’ve searched my code several times, over and over, and cannot find the problem. I must be missing something. Any help or assistance appreciated? Thanks.

Here is a sample of the program execution:

2012-09-17 18:22:49.013 BMITime[405:403] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 1: $153 in assets>",
"<Employee 2: $119 in assets>",
"<Employee 3: $68 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 5: $136 in assets>",
"<Employee 6: $119 in assets>",
"<Employee 7: $34 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 9: $136 in assets>"
)
2012-09-17 18:22:49.014 BMITime[405:403] Giving up ownership of one employee
2012-09-17 18:22:49.015 BMITime[405:403] Giving up ownership of array

[code]Employee.h

#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset;
@interface Employee : Person
{
int employeeID;
NSMutableArray *assets;
}
@property int employeeID;

  • (void) addAssetsObject:(Asset *)a;
  • (unsigned int)valueOfAssets;
    @end
    [/code]

[code]Employee.m

#import “Employee.h”
#import “Asset.h”
@implementation Employee
@synthesize employeeID;

  • (void) addAssetsObject:(Asset *)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 (Asset *a in assets)
    {
    sum += [a resaleValue];
    }
    return sum;
    }

  • (float) bodyMassIndex
    {
    float normalBMI = [super bodyMassIndex];
    return normalBMI * 0.9;
    }

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<Employee %d: $%d in assets>", [self employeeID], [self valueOfAssets]];
    }

  • (void) dealloc
    {
    NSLog(@“deallocating %@”, self);
    }
    @end
    [/code]

Asset.h

#import <Foundation/Foundation.h>
@interface Asset : NSObject
{
NSString *label;
unsigned int resaleValue;
}
@property (strong) NSString *label;
@property unsigned int resaleValue;
@end
Asset.m

#import "Asset.h"
@implementation Asset
@synthesize label, resaleValue;
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: $%d >", [self label], [self resaleValue]];
}
- (void) dealloc
{
NSLog(@"deallocating %@", self);
}
@end

main.m

#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 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];

}

// 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];

// Assign that asset to that employee
[randomEmployee addAssetsObject:asset];

}

NSLog(@"Employees: %@", employees);

NSLog(@"Giving up ownership of one employee");

[employees removeObjectAtIndex:5];

NSLog(@"Giving up ownership of array");

employees = nil; 
}
return 0;
}

#4

The code is correct - however it looks like you don’t have ARC turned on?

I copied your code to double check it and this was the output I got:

2012-09-23 13:06:30.721 BMITime[8285:303] Employees: ( "<Employee 0: $0 in assets>", "<Employee 1: $153 in assets>", "<Employee 2: $119 in assets>", "<Employee 3: $68 in assets>", "<Employee 4: $0 in assets>", "<Employee 5: $136 in assets>", "<Employee 6: $119 in assets>", "<Employee 7: $34 in assets>", "<Employee 8: $0 in assets>", "<Employee 9: $136 in assets>" ) 2012-09-23 13:06:30.726 BMITime[8285:303] Giving up ownership of one employee 2012-09-23 13:06:30.728 BMITime[8285:303] deallocating <Employee 5: $136 in assets> 2012-09-23 13:06:30.729 BMITime[8285:303] deallocating <Laptop 3: $51 > 2012-09-23 13:06:30.730 BMITime[8285:303] deallocating <Laptop 5: $85 > 2012-09-23 13:06:30.730 BMITime[8285:303] Giving up ownership of array 2012-09-23 13:06:30.731 BMITime[8285:303] deallocating <Employee 0: $0 in assets> 2012-09-23 13:06:30.732 BMITime[8285:303] deallocating <Employee 1: $153 in assets> 2012-09-23 13:06:30.733 BMITime[8285:303] deallocating <Laptop 9: $153 > 2012-09-23 13:06:30.734 BMITime[8285:303] deallocating <Employee 2: $119 in assets> 2012-09-23 13:06:30.734 BMITime[8285:303] deallocating <Laptop 7: $119 > 2012-09-23 13:06:30.735 BMITime[8285:303] deallocating <Employee 3: $68 in assets> 2012-09-23 13:06:30.736 BMITime[8285:303] deallocating <Laptop 0: $0 > 2012-09-23 13:06:30.737 BMITime[8285:303] deallocating <Laptop 4: $68 > 2012-09-23 13:06:30.737 BMITime[8285:303] deallocating <Employee 4: $0 in assets> 2012-09-23 13:06:30.738 BMITime[8285:303] deallocating <Employee 6: $119 in assets> 2012-09-23 13:06:30.739 BMITime[8285:303] deallocating <Laptop 1: $17 > 2012-09-23 13:06:30.739 BMITime[8285:303] deallocating <Laptop 6: $102 > 2012-09-23 13:06:30.752 BMITime[8285:303] deallocating <Employee 7: $34 in assets> 2012-09-23 13:06:30.754 BMITime[8285:303] deallocating <Laptop 2: $34 > 2012-09-23 13:06:30.755 BMITime[8285:303] deallocating <Employee 8: $0 in assets> 2012-09-23 13:06:30.756 BMITime[8285:303] deallocating <Employee 9: $136 in assets> 2012-09-23 13:06:30.757 BMITime[8285:303] deallocating <Laptop 8: $136 >

In Xcode go to:
Select Edit -> Refactor… -> Convert to Objective-C ARC

See if that sorts out the issue?


#5

That’s fantastic. Now works perfectly and I appreciate the assistance. Much obliged!


#6

I’m glad you sorted it out. Make sure all your projects have ARC turned on.
Good luck!


#7

Same issue except that Refactor is grayed out. What to do?