Chapter Example - No setLabel


#1

I’m using Xcode 4.3.2 with Lion 10.7.3

I followed the instructions in the book and the program didn’t show the deallocation messages.

For my setup, I can not use this line of code…

@synthesize label, resaleValue;

I have to use -

@synthesize label;
@synthesize resaleValue;

Hope this helps another person… Also, if there’s some setting or adjustment I need to make so the first line of code works, please feel free to let me know…

Thanks.


#2

Nope, I spoke too soon… I’m still getting the following message in my - Main.m - file…

[asset setLabel:currentLabel]; - Instance method ‘-setLabel:’ not found (return type defaults to ‘id’)

[asset setResaleValue:i * 17]; Instance method ‘-setResaleValue:’ not found (return type defaults to ‘id’)

As a result, I’m believe this is why I’m not getting full output of the sample program…

This is my output:

2012-05-11 21:11:13.769 BMITime[456: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-05-11 21:11:13.772 BMITime[456:403] Giving up ownership of one employee
2012-05-11 21:11:13.772 BMITime[456:403] Giving up ownership of array

---- That’s it… I’ll keep looking at it, but I’m open to suggestions… Thanks.


#3

Would you include your Asset.h file, please?


#4

This is my Asset.h file…

#import <Foundation/Foundation.h>

@interface Asset : NSObject
{
    NSString *label;
    unsigned int resaleValue;
    
}

@property (strong) NSString *label;
@property unsigned int resaleValue;

@end

Thanks for the help, I’m fairly stumped…


#5

Was browsing around the forum and realized my ARC wasn’t on, so I fixed that and my project failed to build.

Specifically in main.m, I received errors for the following:

        [asset setLabel:currentLabel]; - ARC Issue - No visible @interface for 'Asset' declares the selector 'setLabel:'

 
        [asset setResaleValue:i * 17]; - ARC Issue - No visible @interface for 'Asset' declares the selector 'setResaleValue:'

I get the same error if I use lowercase letters for setlabel and setresaleValue.

Only two issues with the program preventing it from building (to my knowledge).


#6

Switched to ARC by commenting out the following lines of code in main.m

// NSString *currentLabel = [NSString stringWithFormat:@“Laptop %d”, i];

    // [asset setLabel:currentLabel];
        
    // [asset setResaleValue:i * 17];

This allowed ARC to work and the program to build but the output is:

2012-05-12 10:07:14.625 BMITime[316:403] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 1: $0 in assets>",
"<Employee 2: $0 in assets>",
"<Employee 3: $0 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 5: $0 in assets>",
"<Employee 6: $0 in assets>",
"<Employee 7: $0 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 9: $0 in assets>"
)
2012-05-12 10:07:14.629 BMITime[316:403] Giving up ownership of one employee
2012-05-12 10:07:14.631 BMITime[316:403] deallocating <Employee 5: $0 in assets>
2012-05-12 10:07:14.632 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.633 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.635 BMITime[316:403] Giving up ownership of array
2012-05-12 10:07:14.636 BMITime[316:403] deallocating <Employee 0: $0 in assets>
2012-05-12 10:07:14.637 BMITime[316:403] deallocating <Employee 1: $0 in assets>
2012-05-12 10:07:14.638 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.640 BMITime[316:403] deallocating <Employee 2: $0 in assets>
2012-05-12 10:07:14.641 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.642 BMITime[316:403] deallocating <Employee 3: $0 in assets>
2012-05-12 10:07:14.644 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.645 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.647 BMITime[316:403] deallocating <Employee 4: $0 in assets>
2012-05-12 10:07:14.648 BMITime[316:403] deallocating <Employee 6: $0 in assets>
2012-05-12 10:07:14.650 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.652 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.653 BMITime[316:403] deallocating <Employee 7: $0 in assets>
2012-05-12 10:07:14.654 BMITime[316:403] deallocating <(null): $0 >
2012-05-12 10:07:14.655 BMITime[316:403] deallocating <Employee 8: $0 in assets>
2012-05-12 10:07:14.656 BMITime[316:403] deallocating <Employee 9: $0 in assets>
2012-05-12 10:07:14.657 BMITime[316:403] deallocating <(null): $0 >


#7

anyone have a working copy of the main.m code?

Here’s my main.m code and the Create 10 assets section isn’t working for me. Please help.

[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 interesting values
    
    [person setWeightInKilos:90 + i];
    [person setHeightInMeters:1.8 - i / 10.0 ];
    [person setEmployeeID:i];
    
    // Put the employee in the array
        
        [employees addObject:person];
        
    }
        
    // Create 10 assets
    
    for (int i = 0; i < 10; i++) {
    
        
       Asset *asset = [[Asset alloc] init];
        
        // Give it an interesting label
        
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
        
    [asset setLabel: currentLabel]; //This line gives me errors
        
    [asset setResaleValue: i * 17]; // This line gives me errors
        
        
    // Get a random number between 0 & 9 inclusive
        
        NSUInteger randomIndex = random() % [employees count];
        
        // Find that employee
        
        Employee *randomEmployee = [employees objectAtIndex:randomIndex];
        
        // Assign the asset to the 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;

}

[/code]


#8

Ok. I can talk to myself… no problem

Tried to advance to Chapter 20. Without this program functioning in Chapter 19, it’s useless in Chapter 20… So, I’m stuck.
I’ve copied and compared code from Chapter 20 and Chapter 19 forum postings. Seriously frustrated.

ARC is on - Lion 10.7.4 - Xcode - 4.3.2

Project would not build. I went into the Build Settings - Targets - BMITime - and changed Objective-C Automatic Reference Counting - to OFF.

Project built, but I got four issues.

main.m
Semantic Issue - Instance method ‘-setLabel:’ not found (return type defaults to ‘id’)
Semantic Issue - Instance method ‘-setResaleValue:’ not found (return type defaults to ‘id’)

Employee.m
Method possibly missing a [super dealloc] call

Asset.m
Method possibly missing a [super dealloc] call

The output is:

2012-05-14 02:39:40.764 BMITime[538:403] Employees: (
"<Employee 0 has $0 in assets>",
"<Employee 1 has $153 in assets>",
"<Employee 2 has $119 in assets>",
"<Employee 3 has $68 in assets>",
"<Employee 4 has $0 in assets>",
"<Employee 5 has $136 in assets>",
"<Employee 6 has $119 in assets>",
"<Employee 7 has $34 in assets>",
"<Employee 8 has $0 in assets>",
"<Employee 9 has $136 in assets>"
)
2012-05-14 02:39:40.766 BMITime[538:403] Giving up ownership of one employee
2012-05-14 02:39:40.785 BMITime[538:403] Giving up ownership of array

I turned off the Objective-C Automatic Reference Counting to No in the Build Settings of the Project - BMITime and got the same results concerning warnings and output.

3 am…

Commented out the two trouble lines - setLabel & setResaleValue, then turned Objective-C ARC to Yes in the Target of BMITime. Got this result:

2012-05-14 03:04:24.514 BMITime[621:403] Employees: (
"<Employee 0 has $0 in assets>",
"<Employee 1 has $0 in assets>",
"<Employee 2 has $0 in assets>",
"<Employee 3 has $0 in assets>",
"<Employee 4 has $0 in assets>",
"<Employee 5 has $0 in assets>",
"<Employee 6 has $0 in assets>",
"<Employee 7 has $0 in assets>",
"<Employee 8 has $0 in assets>",
"<Employee 9 has $0 in assets>"
)
2012-05-14 03:04:24.526 BMITime[621:403] Giving up ownership of one employee
2012-05-14 03:04:24.527 BMITime[621:403] deallocating <Employee 5 has $0 in assets>
2012-05-14 03:04:24.528 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.529 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.529 BMITime[621:403] Giving up ownership of array
2012-05-14 03:04:24.530 BMITime[621:403] deallocating <Employee 0 has $0 in assets>
2012-05-14 03:04:24.531 BMITime[621:403] deallocating <Employee 1 has $0 in assets>
2012-05-14 03:04:24.531 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.532 BMITime[621:403] deallocating <Employee 2 has $0 in assets>
2012-05-14 03:04:24.534 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.534 BMITime[621:403] deallocating <Employee 3 has $0 in assets>
2012-05-14 03:04:24.535 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.535 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.536 BMITime[621:403] deallocating <Employee 4 has $0 in assets>
2012-05-14 03:04:24.537 BMITime[621:403] deallocating <Employee 6 has $0 in assets>
2012-05-14 03:04:24.538 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.538 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.539 BMITime[621:403] deallocating <Employee 7 has $0 in assets>
2012-05-14 03:04:24.540 BMITime[621:403] deallocating <Asset (null): $0 unassigned>
2012-05-14 03:04:24.540 BMITime[621:403] deallocating <Employee 8 has $0 in assets>
2012-05-14 03:04:24.541 BMITime[621:403] deallocating <Employee 9 has $0 in assets>
2012-05-14 03:04:24.542 BMITime[621:403] deallocating <Asset (null): $0 unassigned>

Removed the comments for those two lines and the project failed to build…

[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++)
    {
        Employee *person = [[Employee alloc] init];
        // Assign values to the instance variables
        [person setWeightInKilos: 90 + i];
        [person setHeightInMeters:1.8 - i/10.0];
        [person setEmployeeID:i];
        
        [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]; // My problem line
        [asset setResaleValue:i * 17]; // My problem line
        
        //Get a random number between 0 and 9 inclusive
        NSUInteger randomIndex = random() % [employees count];
        
        //Find that employee
        Employee *randomEmployee = [employees objectAtIndex:randomIndex];
        
        //Assgin the asset to the 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;

}

[/code]

[code]//
// Asset.h
// BMITime
//

#import <Foundation/Foundation.h>

@class Employee;
@interface Asset : NSObject
{
NSString *label;
__weak Employee *holder;
unsigned int resaleValue;
}

@property (strong) NSString *label;
@property (weak) Employee *holder;
@property unsigned int resaleValue;
@end

[/code]
Some code is in there because I tried to proceed to the next chapter… Is this an issue with Apple LLVM compiler 3.1 ?

[code]//
// Asset.m
// BMITime
//

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

@implementation Asset
@synthesize label;
@synthesize resaleValue;
@synthesize holder;

  • (NSString *)description
    {
    if ([self holder]) {
    return [NSString stringWithFormat:@"<Asset %@: %d, assigned to %@>", [self label], [self resaleValue], [self holder]]; } else { return [NSString stringWithFormat:@"<Asset %@: %d unassigned>", [self label], [self resaleValue]];
    }
    }

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

Sigh… Put Objective-C ARC - to YES in the Project and Put Objective-C - to NO in the Targets. Still built with four 'issues" -

2012-05-14 03:22:49.654 BMITime[740:403] Employees: (
"<Employee 0 has $0 in assets>",
"<Employee 1 has $153 in assets>",
"<Employee 2 has $119 in assets>",
"<Employee 3 has $68 in assets>",
"<Employee 4 has $0 in assets>",
"<Employee 5 has $136 in assets>",
"<Employee 6 has $119 in assets>",
"<Employee 7 has $34 in assets>",
"<Employee 8 has $0 in assets>",
"<Employee 9 has $136 in assets>"
)
2012-05-14 03:22:49.678 BMITime[740:403] Giving up ownership of one employee
2012-05-14 03:22:49.682 BMITime[740:403] Giving up ownership of array

Sigh… 3:30 … if this book wasn’t on my iPad, I’d have thrown it across the room a few times.


#9

Sigh…

When in Doubt… clean the slate.

I started over with a new project called BMITime2 with ARC checked…

Fortunately or thankfully, Kyashan, posted all the code for the project in this thread: viewtopic.php?f=151&t=3759 - so I copied it over.

To my surprise, shock, amazement - There aren’t any errors and I got the proper output.

I still don’t know why the old code didn’t work properly… I don’t even want to think about it…

Time to get 3 hours of sleep and get ready for work.


#10

I wonder if it could be this simple?

The two problem lines have a small semantic error-- namely there is a white space after the colon.

Here’s what you’ve got:

[code] [asset setLabel: currentLabel]; //This line gives me errors

    [asset setResaleValue: i * 17]; // This line gives me errors[/code]

Try this and see if it possibly helps:

[code] [asset setLabel:currentLabel];

    [asset setResaleValue:i * 17]; [/code]

I found that I needed to activate ARC for the project in order for the code to run correctly, also.