Trouble in BMITime


#1

For some reason in my main.m file

When i create the:

// Create an Asset
Asset *asset = [[Asset alloc] init];

// GIve it an interesting label
NSString *currentLabel = [NSString stringWithFormat:@“Laptop %d”, i];
[asset setLabel: currentLabel]; I am getting an issue on this line and
[asset setResaleValue: i * 17]; on this line saying "No visible @interface for ‘Asset’ declares the selector ‘setLabel’, and then on this line it says ‘setResaleValue’

What is the issue you think?? I cannot figure it out.

Person.h file

#import <Foundation/Foundation.h>

// The class Person inherits all the instance variables and methods defined by the class NSObject
@interface Person : NSObject
{
   // It has two instance variables
   float heightInMeters;
   int weightInKilos;
}

/*
// You will be able to set those instance variables using these methods
- (float)heightInMeters;
- (void)setHeightInMeters:(float)h;
- (int)weightInKilos;
- (void)setWeightInKilos:(int)w;   // these are setter and getter methods
 */

@property float heightInMeters; // The @property replaces the methods declared above. setter and getter methods all in one line
@property int weightInKilos;

// This method calculates the Body Mass Index
- (float)bodyMassIndex;

@end

Person.m file

#import "Person.h"

@implementation Person

@synthesize heightInMeters, weightInKilos; // This is making the syntax easier than typing all that out below. simpler text

/*
- (float)heightInMeters
{
    return heightInMeters;
}

- (void)setHeightInMeters:(float)h
{
    heightInMeters = h;
}

- (int)weightInKilos
{
    return weightInKilos;
}

- (void)setWeightInKilos:(int)w
{
    weightInKilos = w;
}

 */

- (float)bodyMassIndex
{
    // return weightInKilos / (heightInMeters * heightInMeters);
    float h = [self heightInMeters];
    return [self weightInKilos] / (h * h); // This is another way to write what i did in line 40. using the local variable 'self'
}

@end

Employee.h file

#import <Foundation/Foundation.h>
#import "Person.h"
@class Asset; // This removes the error from line 20 for now. Shows that there is in fact a class called 'Asset'

@interface Employee : Person
{
    int employeeID;
    NSMutableArray *assets;
}

@property int employeeID;
- (void)addAssetsObject:(Asset *)a;
- (unsigned int)valueOfAssets;

@end

Employee.m file

#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 file

#import <Foundation/Foundation.h>

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

@property (strong) NSString *label; // (strong) means 'This is a pointer to an object upon which I claim ownership'
@property unsigned int resaleValue;


@end

Asset.m file

#import "Asset.h"

@implementation Asset

@synthesize label, resaleValue;

- (NSString *)description  // the 'description' method from NSObject returns a string that is a useful description of an instance of the class. 
{
    return [NSString stringWithFormat:@"<%@: $%d >", [self label], [self resaleValue]];
    // I used the 'description' method to return a string that includes the instance's 'label' and 'resaleValue'
}

- (void)dealloc
{
    NSLog(@"deallocating %@", self);
}
@end

main.m file

#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 employee 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 employeee
            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;
        
        // Create an instance of Person
        Employee *person = [[Employee alloc] init];
        // id person = [[Person alloc] init];
        // Person *person = [[Person alloc] init];
        // Person *sally = [[Person alloc] init];
        
        // Give the instance variables interesting values
        [person setWeightInKilos:96];
        [person setHeightInMeters:1.8];
        [person setEmployeeID: 15];
        
        
        
        // Call the bodyMassIndex method
        float bmi = [person bodyMassIndex];
        NSLog(@"Employee %d has a BMI of %f", [person employeeID], bmi);
        
        
        
    }
    return 0;
}

#2

I have created a project from your files, and I did not get any warnings from Xcode.

How did you create your project? Did you create it from scratch, or did you cannibalise other projects?

To see if you will still get the same warning, try creating a new project by adding the files from this post.


#3

I created the project again and did a command line tool like it said in the book. It still isn’t working for some reason. I am very confused!


#4

Nevermind! I went through and copied and pasted AGAIN and it somehow worked that time. Weird stuff haha