Browse files

Custom initializers

  - Changed to use accessor's instead of ivars
  - Removed redundant ivars
  - Switched main code to use new init methods
  • Loading branch information...
1 parent bd255b1 commit 25081fba1440d32f6726e66abbafc3264a39f253 Ryan Blunden committed Jan 13, 2013
View
10 Business/Business/Employee.h
@@ -10,16 +10,14 @@
@class Asset;
-@interface Employee : Person {
- int employeeId;
- Person *spouse;
- NSMutableArray *children;
- NSMutableSet *assets;
-}
+@interface Employee : Person
@property int employeeId;
@property Person *spouse;
@property NSMutableArray *children;
+@property NSMutableSet *assets;
+
+- (id)initWithEmployeeId:(int)eId;
- (void)addAssetObject:(Asset *)a;
View
17 Business/Business/Employee.m
@@ -11,29 +11,36 @@
@implementation Employee
-@synthesize employeeId, spouse, children;
+- (id)initWithEmployeeId:(int)eId {
+ self = [super init];
+ if (self) {
+ [self setEmployeeId:eId];
+ }
+
+ return self;
+}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ %@ - $%d>", [self firstName], [self lastName], [self valueOfAssets]];
}
- (void)addAssetObject:(Asset *)a {
// Is assets nil?
- if (!assets) {
- assets = [[NSMutableSet alloc] init];
+ if (![self assets]) {
+ [self setAssets:[[NSMutableSet alloc] init]];
}
// Check is asset before adding
if ([a isKindOfClass:[Asset class]]) {
- [assets addObject:a];
+ [[self assets] addObject:a];
[a setHolder:self];
}
}
- (unsigned int)valueOfAssets {
// Sum up the resale value of assets
unsigned int sum = 0;
- for (Asset *asset in assets) {
+ for (Asset *asset in [self assets]) {
sum += [asset resaleValue];
}
View
9 Business/Business/Person.h
@@ -8,10 +8,11 @@
#import <Foundation/Foundation.h>
-@interface Person : NSObject {
- NSString *firstName;
- NSString *lastName;
-}
+@interface Person : NSObject
+
+- (id)init;
+
+- (id)initWithFirstName:(NSString *)fn lastName:(NSString *)ln;
@property NSString *firstName;
@property NSString *lastName;
View
14 Business/Business/Person.m
@@ -10,6 +10,18 @@
@implementation Person
-@synthesize firstName, lastName;
+- (id)init {
+ return [self initWithFirstName:@"" lastName:@""];
+}
+
+- (id)initWithFirstName:(NSString *)fn lastName:(NSString *)ln {
+ self = [super init];
+ if (self) {
+ [self setFirstName:fn];
+ [self setLastName:ln];
+ }
+
+ return self;
+}
@end
View
11 Business/Business/main.m
@@ -40,15 +40,13 @@ int main(int argc, const char *argv[]) {
// Create an array of Executive objects
NSMutableDictionary *executives = [[NSMutableDictionary alloc] init];
- NSArray *executivePositions = [NSArray arrayWithObjects:@"CEO", @"CTO", @"VP", @"Director", @"Manager"];
+ NSArray *executivePositions = [NSArray arrayWithObjects:@"CEO", @"CTO", @"VP", @"Director", @"Manager", nil];
for (NSString *position in executivePositions) {
- Employee *exec = [[Employee alloc] init];
+ Employee *exec = [[Employee alloc] initWithFirstName:[names objectAtIndex:arc4random() % [names count]]
+ lastName:[names objectAtIndex:arc4random() % [names count]]];
[executives setObject:exec forKey:position];
- exec.firstName = [names objectAtIndex:arc4random() % [names count]];
- exec.lastName = [names objectAtIndex:arc4random() % [names count]];
-
// Execs get company cars
Asset *car = [[Asset alloc] init];
car.label = @"Audi";
@@ -65,7 +63,8 @@ int main(int argc, const char *argv[]) {
for (int i = 0; i < 10; i++) {
// Create an instance of Employee
- Employee *employee = [[Employee alloc] init];
+ Employee *employee = [[Employee alloc] initWithFirstName:[names objectAtIndex:arc4random() % [names count]]
+ lastName:[names objectAtIndex:arc4random() % [names count]]];
// Add employee to employee array
[employees addObject:employee];

0 comments on commit 25081fb

Please sign in to comment.