To save some money, I hold an AT&T family account and added 4 of my friends as family member lines. Each month, I do the tedious work of splitting the bill by subtracting individual usage (SMS, data, features, etc.) and divide the rest equally among the five of us, and add the individual amount back. Not only that, I would also author an email with a semi-template to fill in those numbers and send to everyone for them to pay online.
So I said, why don’t I spend my weekend-hobbyist energy this week on some utility to help me do this. Moreover, since it is so simple a task, why don’t I make fun by using a language that I never touched before? So here it is, the AT&T Bill Splitter, my first utility app on Mac besides those cross-platform ones done in Python or Java, implemented in Objective-C and Cocoa on Mac.
The application is just as simple as it is. I feed in the necessary values, while the most of the entries that do not vary on a month by month basis are even greyed out, and the app will generate an email message with the body generated for me to paste into emails. (Yes, I can actually also invoke the Mail application and send the content directly, but let me do that later.)
The process of creating the app was enjoyable: the MVC framework in Cocoa was even simpler, as soon as the notions of
Outlets were clear. The rest work was merely manipulating strings.
Interestingly, Objective-C has a notorious feature of being “self-documenting”, which means a lot of typing, I mean, an extremely lot. The code for concatenating two
NSStrings, for example, was a call to the
stringByAppendingString: method of one of the strings. This uses the following code.
newString = [oldString1 stringByAppendingString: oldString2];
This method is so long but so frequently used in my utility that I even suspected if I had the right programming practice in the Cocoa world, so I googled. To my surprise, it seems it is the way! To obtain a simpler use of the string appending features such as
oldString1 + oldString2 in Python or
oldString1.append(oldString2) in Java, people have posted more than a dozen solutions in this StackOverflow post, and the most popular answer has more than 300 votes, while a total of more than 600 people have voted. Not to mention those curious minds who Googled to this page, read, and left.
To be honest, I may be exaggerating. There is indeed a
stringWithFormat: which is shorter, and construct a string directly as in the
printf statement in the C world, with the following syntax.
NSString* newString = [NSString stringWithFormat:@"%d Mississippi and %f Mississippis!", 1, 2.0];
However this does not apply to my case due to such a long message being constructed. Purely using
stringWithFormat: would result in a single method call being more then 30 lines long, considering one variable for each of the many blanks in the message I have to fill in!
Nonetheless, I still like the way this level of verbosity Objective-C provides—it is after all much more succinct and strict than what AppleScript uses, as I experienced when writing a utility to convert images files to various output formats automatically.
There are yet many many more aspects in Cocoa programming that I haven’t touched. They will be explored in my potential future weekend hobbyist endeavors. In particular to this small utility, sending emails from within the application as well as further refactoring the code can be next.