I have been using the code from this chapter and putting it into my BMITime project but keep getting issues with it that I can’t figure out myself. I was just wondering if anyone could help me with this. I am new to Objective-C and this has been a big learning curve for me but when I do make mistakes and then see the right way to do implement the code it suddenly clicks with me.
Here’s my code.
Main.m:
[code]//
// main.m
// BMITime
//
// Created by Thomas Paterson on 28/05/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import “BNREmployee.h”
#import “BNRAsset.h”
int main(int argc, const char * argv[])
{
@autoreleasepool {
//create an array of BNREmployee objects
NSMutableArray *employees = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++) {
//Create an instance
BNREmployee *mikey = [[BNREmployee alloc]init];
//Give values to the variables
mikey.weightInKilos = 90 + 1;
mikey.heightInMeters = 1.8 - i/10.0;
mikey.employeeID = i;
//Put in the array
[employees addObject:mikey];
}
//Creat 10 assets
for (int i = 0; i < 10; i++) {
//Create an asset
BNREmployee *asset = [[BNRAsset alloc]init];
NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
asset.label = currentLabel;
asset.resaleValue = 360 + i * 17;
//Create a random number between 0-9 inclusive
NSUInteger randomIndex = random() % [employees count];
//Find that employee
BNREmployee *randomEmployee = [employees objectAtIndex:randomIndex];
//Assign the asset to the employee
[randomEmployee addAsset:asset];
}
NSLog(@"Employees %@", employees);
NSLog(@"Giving up ownership of one employee");
[employees removeObject:5];
NSLog(@"Giving up ownership of arrays");
employees = nil;
}
return 0;
}
[/code] Errors: ‘Property ‘label’ not found on object of type BNREmployee’ and ‘Property ‘resaleValue’ not found on object of type BNREmployee’
BNREmployee.h:
[code]//
// BNREmployee.h
// BMITime
//
// Created by Thomas Paterson on 30/05/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import “BNRPerson.h”
@interface BNREmployee : BNRPerson
{
NSMutableArray *_assets;
}
@property (nonatomic) unsigned int employeeID;
@property (nonatomic) unsigned int officeAlarmCode;
@property (nonatomic) NSDate *hireDate;
@property (nonatomic, copy) NSArray *assets;
- (double)yearsOfEmployment;
- (void)addAsset:(BNRAsset *)a;
- (unsigned int)valueOfAssets;
@end
[/code] Errors: for this line ‘- (void)addAsset:(BNRAsset *)a;’ Expected a type
BNREmployee.m:
[code]//
// BNREmployee.m
// BMITime
//
// Created by Thomas Paterson on 30/05/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import “BNREmployee.h”
#import “BNRAsset.h”
@implementation BNREmployee
//Accessors for assets properties
-
(void)setAssets:(NSArray *)a
{
_assets = [a mutableCopy];
} -
(NSArray *)assets
{
return [ _assets copy];
} -
(void)addAsset:(BNRAsset *)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 (BNRAsset *a in _assets){
sum += [a resaleValue];
}
return sum;
} -
(double)yearsOfEmployment
{
if (self.hireDate) {
NSDate *now = [NSDate date];
NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate];
return secs / 31557600.0;
} else {
return 0;
}
} -
(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]
BNRPerson.h:
[code]//
// BNRPerson.h
// BMITime
//
// Created by Thomas Paterson on 28/05/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BNRPerson : NSObject
@property (nonatomic) float heightInMeters;
@property (nonatomic) int weightInKilos;
//BNRPerson has a method that calculates the Body Mass Index
- (float)bodyMassIndex;
@end
[/code]
BNRPerson.m:
[code]//
// BNRPerson.m
// BMITime
//
// Created by Thomas Paterson on 28/05/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import “BNRPerson.h”
@implementation BNRPerson
- (float)bodyMassIndex
{
float h = [self heightInMeters];
return [self weightInKilos] / (h * h);
}
@end
[/code]
BNRAsset.h:
[code]//
// BNRAsset.h
// BMITime
//
// Created by Thomas Paterson on 04/06/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import “BNREmployee.h”
@interface BNRAsset : NSObject
@property (nonatomic, copy) NSString *label;
@property (nonatomic) unsigned int resaleValue;
@end
[/code]
BNRAsset.m:
[code]//
// BNRAsset.m
// BMITime
//
// Created by Thomas Paterson on 04/06/2014.
// Copyright © 2014 Big Nerd Ranch. All rights reserved.
//
#import “BNRAsset.h”
@implementation BNRAsset
-
(NSString *)description
{
return [NSString stringWithFormat:@"<%@: $d>", self.label, self.resaleValue];
} -
(void)dealloc
{
NSLog(@“deallocating %@”, self);
}
@end
[/code]
Thank you in advance