Here is my code, if that helps.
BNRAsset.h
[code]#import <Foundation/Foundation.h>
@interface BNRAsset : NSObject
@property (nonatomic, copy) NSString *label;
@property (nonatomic) unsigned int resaleValue;
@end[/code]
BNRAsset.m
[code]#import “BNRAsset.h”
@implementation BNRAsset
-
(NSString *)description
{
return [NSString stringWithFormat:@"<%@: $%u>", self.label, self.resaleValue];
}
-
(void)dealloc
{
NSLog(@“deallocating %@”, self);
}
@end[/code]
BNREmployee.h
[code]#import “BNRPerson.h”
@class BNRAsset;
@interface BNREmployee : BNRPerson
@property (nonatomic) unsigned int employeeID;
@property (nonatomic) NSDate *hireDate;
@property (nonatomic, copy) NSArray *assets;
- (double)yearsOfEmployment;
- (void)addAsset:(BNRAsset *)a;
- (void)removeAsset:(BNRAsset *)r;
- (unsigned int)valueOfAssets;
@end[/code]
BNREmployee.m
[code]#import “BNREmployee.h”
#import “BNRAsset.h”
// A class extension
@interface BNREmployee()
{
NSMutableArray *_assets;
}
@property (nonatomic) unsigned int officeAlarmCode;
@end
@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];
}
-
(void)removeAsset:(BNRAsset *)r
{
// Is assets nil?
if (_assets) {
[_assets removeObject:r];
}
}
-
(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
{
// Do I have a non-nil hireDate?
if (self.hireDate) {
// NSTimeInterval is the same as double
NSDate *now = [NSDate date];
NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate];
return secs / 31557600.0; // Seconds per year
} else {
return 0;
}
}
-
(float)bodyMassIndex
{
float normalBMI = [super bodyMassIndex];
return normalBMI * 0.9;
}
-
(NSString *)description
{
return [NSString stringWithFormat:@"<Employee %u: $%u in assets, %@>", self.employeeID, self.valueOfAssets, self.assets];
}
-
(void)dealloc
{
NSLog(@“deallocating %@”, self);
}
@end[/code]
main.m
[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 variables interesting values
mikey.weightInKilos = 90 + i;
mikey.heightInMeters = 1.8 - i/10.0;
mikey.employeeID = i;
// Put the employee in the employees array
[employees addObject:mikey];
}
// Create 10 assets
for (int i = 0; i < 10; i++) {
// Create an asset
BNRAsset *asset = [[BNRAsset alloc] init];
// Give it an interesting label
NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
asset.label = currentLabel;
asset.resaleValue = 350 + i * 17;
// Get a random number between 0 and 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 removeObjectAtIndex:5];
NSLog(@"Giving up ownership of arrays");
employees = nil;
NSLog(@"Removing all assets");
for (BNREmployee *e in employees) {
for (BNRAsset *a in e.assets) {
[e removeAsset:a];
}
}
}
return 0;
}[/code]