Hey everyone, hope everyone is having a good week!
so I have been tackling chapter 21 without too many problems until the very end, just before the challenge. I get the error: Property ‘weightInKilos’ not found on Object Type “BNREmployee”. I went back to the inheritance chapter, and I just cannot find the issue.
Edit: I keep trying to find the issue, it seems like BNREmployee isn’t linked,or a subclass of BNRPerson, but when I look at my code it seems like BNREmployee IS a subclass of BNRPerson.
Here is my code:
BNR Employee.h
[code]#import “BNRPerson.h”
//link BNRAsset to BNREmployee.
@class BNRAsset;
@interface BNREmployee:BNRPerson
//here we create our a new instance variable to contain a pointer to the mutable array that we will create.And a couple of new methods.
{
NSMutableArray *_assets;
}
@property(nonatomic)int employeeId;
@property(nonatomic)int officeAlarmCode;
@property(nonatomic)NSDate *hiredate;
//here is our instance variable that contain the pointer
@property(nonatomic,copy) NSArray *assets;
-(double)yearsOfEmployment;
// and here are our new methods
//this one is to add an asset to the array
- (void)addAssets:(BNRAsset *)a;
//this one is to sum up the resale value of assets
-(unsigned int)valueOfAssets;
@end[/code]
BNREmployee.m
[code]#import “BNREmployee.h”
//here we need to import our new class to access it
#import “BNRAsset.h”
@implementation BNREmployee
//accessors for assets properties
-(void)setAssets:(NSArray *)a
{
_assets=[a mutableCopy];
}
-(NSArray *)assets
{
return [_assets copy];
}
-(void)addAssets:(BNRAsset *)a
{
//is asset nil?
if(!_assets){
//create a array
_assets=[[NSMutableArray alloc]init];
}
[_assets addObject:a];
}
- (unsigned int)valueOfAssets
{
//Sum up the resale value of assets
unsigned int sum=0;
for(BNRAsset *a in _assets){
sum+=[a resaleValue];
}
return sum;
}
//description of the employee’s assets.
-(NSString *)description
{
return [NSString stringWithFormat:@"<Employee %d: $%d in assets>", self.employeeId,self.valueOfAssets];
}
//method to announce when deallocating memory
-(void)dealloc
{
NSLog(@“deallocating %@”, self);
}
-(double)yearsOfEmployment
{
//do I have a non-null hire date?
if(self.hiredate){
NSDate*now=[NSDate date];
NSTimeInterval secs=[now timeIntervalSinceDate:self.hiredate];
return secs/31557600.0;
}else{
return 0;
}
}
@end
[/code]
BNRPerson.h
[code]#import <Foundation/Foundation.h>
@interface BNRPerson : NSObject
@property(nonatomic) float heightInMeters;
@property(nonatomic) int weightInKilos;
//BNRPerson method tp calculate BMI
- (float)bodyMassIndex;
@end
[/code]
BNRPerson.m
[code]#import “BNRPerson.h”
@implementation BNRPerson
- (float)bodyMassIndex
{
return _weightInKilos / (_heightInMeters*_heightInMeters);
}
@end[/code]
and my Main since I doubt it has anything to do with BNRAsset right?
[code]#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 of BNREmployee
BNREmployee *mikey= [[BNREmployee alloc]init];
//Give the instance varialble (mikey's variables) interesting values
mikey.weightInKilos=90+i;
}
}
return 0;
}
[/code]
Thanks for the help everyone, and by the way the book is amazing!
Charles