Wednesday, October 14, 2009

Instant Cocoa: Checkbook project 4




Loading some data to start


I like to load some fake data to work on, at least for development. We talked about how to do that the other day. We use a plist file.

Here is the code to load it:


- (void)awakeFromNib {
NSLog(@"MyDocument awakeFromNib");
if (nil == checkbookItemsArray) {
NSLog(@"no data yet---new window");
NSString *fn = [[NSBundle mainBundle]
pathForResource:@"fakeData" ofType:@"plist"];
NSURL *fileURL = [NSURL fileURLWithPath:fn];
dataIn =[NSMutableArray arrayWithContentsOfURL:fileURL];
[self convertFakeDataToCheckbookItemsArray:dataIn];
}
}


We allocate and init CheckbookItem objects and fill in their data variables. These are stored in the @synthesized variable (in MyDocument), checkbookItemsArray. This code is duplicated to some extent elsewhere, but I haven't fixed that yet.

We also register ourselves as observers for certain keys in the objects we've made.

Now that we have some data, we are ready for the User Interface and some pretty screenshots.


- (void)convertFakeDataToCheckbookItemsArray:
(id)dataIn {
NSArray *oKeys = [NSArray
arrayWithObjects:@"amount",@"clear",@"isDeposit",nil];
temp = [NSMutableArray arrayWithCapacity:5];
for (mD in dataIn) {
cbi = [[CheckbookItem alloc] init];
[cbi setName:[mD objectForKey:@"name"]];
[cbi setAmount:[mD objectForKey:@"amount"]];
[cbi setIsDeposit:[mD objectForKey:@"isDeposit"]];
[cbi setCheckNumber:[mD objectForKey:@"checkNumber"]];
[cbi setClear:[mD objectForKey:@"clear"]];
[cbi setBalance:[mD objectForKey:@"balance"]];
[cbi setDate:[NSDate date]];
for (NSString *key in oKeys) {
[cbi addObserver:self
forKeyPath:key
options:NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld
context:[cbi checkNumber]];
}
[temp addObject:cbi];
}
[self setCheckbookItemsArray:temp];
}

No comments: