Wednesday, October 14, 2009

Instant Cocoa: Checkbook project 7

Finishing touches

I'm just about done with this series. Two final points, and I will put up the project for anyone who's interested and post instructions for getting it.

The first thing is the code to decide if a given name is a deposit or not. We have an array called:

searchTerms = [NSArray arrayWithObjects:
@"deposit",@"interest", @"dep",@"int",@"balance forward",nil];
[searchTerms retain];

We'll go through the array testing for whether the string is found in the name of a new item, using NSString's method rangeOfString: options:. This could be better. We require the match to be at the beginning of the name. The reason is that I want my checks to "Sprint" not to count as an "int" (interest, a positive transaction). Probably what I should do is set up a UserPref that sets the magic words...

- (NSNumber *)isThisADeposit:(NSString *)s {
NSString *t;
for (t in searchTerms) {
NSRange r = [s rangeOfString:t
// despite the fun of NSNotFound
//if (r.location != NSNotFound) { return YES; }
// this tests for it at the begining
if (!(r.location)) {
return [NSNumber numberWithBool:YES];
return [NSNumber numberWithBool:NO];

The other interesting detail is to set up autonumbering for the checks. If we're adding at the end of the array, we want to search for the last check previous. Because of the display issue mentioned before (number v. empty string), these values are strings. That makes the math a bit roundabout:

- (NSString *)lastCheckNumberPlusOne{
i = [checkbookItemsArray count] - 1;
for (i ; i > -1; i--) {
if (!([[checkbookItemsArray objectAtIndex:i]
checkNumber] == @"")) {
if (i == -1) { return @"1"; }
NSString *s = [[checkbookItemsArray
objectAtIndex:i] checkNumber];
return [NSString stringWithFormat:@"%d",
[s intValue] + 1];

One more post.

No comments: