Wednesday, October 14, 2009

Instant Cocoa: Checkbook project 1



As I mentioned before, these Instant Cocoa* posts are primarily intended to provide simple working examples of common Cocoa constructs, and also to let Google help organize my head. However, just so you know, I am not a Cocoa guru, and I don't play one on TV. The person you are looking for is Aaron Hillegass.

I want to do something a little more involved in the next few posts.

I just made a new version of my (often mentioned) Cocoa checkbook application. I think it's simple, functional and fairly easy to get your head around. So, if you're just learning Cocoa, have done the Currency Converter tutorial (both with and without bindings) and perhaps explored making pretty shapes in a custom view, follow along at home.

[Note: I've only built the project under Snow Leopard. The custom number and date formatters are at least 10.4+, and maybe 10.5. YMMV ]


Overview of the project

• We aim to simplify common tasks. The first goal of the app is to allow you to make entries easily. So, all that is needed is to enter an item description (the "name") as well as an amount. In processing this, we deduce whether the item is a check or a deposit by looking for keywords (an idea for the future is to allow customization of this). The amount will be color-coded when it is displayed. Sequential check numbers can be added automatically.

• Second, it is trivial to balance the checkbook against a bank statement. Items can be marked (later) when they have cleared the bank. The balance can be computed in either of two modes---counting only cleared items, or counting all the items.

• Users may edit any entry, add an entirely new one at a selected location, or delete a selected item.

We use bindings to keep track of the item edits in the table, as well as to color-code the values. However, the balance entry for each row will depend on the mode we're in, as well as whether a user has edited the amount of an entry (or above it) or even an "isCheck" attribute.

This means we need to be notified when one of these values for an item changes, and that requires (I think) that we use a class to represent each entry, rather than a simple dictionary. I haven't been able to get registered as an observer otherwise.


In addition

• This document-based app allows saving and loading of files, and it's easy to accomplish. I decided to encode the checkbook data as an NSData object. A nice benefit of this approach is that the observer relationships are retained through the save and reload process. The disadvantage is that the file contains data---gibberish to a text editor. But we will add an export function later, and may revisit this issue. There's something to be said for plists and even csv files.

• I usually put the code at the end of the post, but that approach makes it harder to see what I'm actually talking about at each point. The project is complex enough that a complete code listing for one class won't get you that far, so I plan to put the project in my Public folder at .mac. As Norm says, you'll hear more about that before this program ends. See you next time.

[UPDATE: Here is the download page hosted at my .mac pages.]

* Note on the name: I was pleased with myself for having thought of this name, which seems to fit perfectly. However, somebody has already thought of it! And they look to have some very nice products.