haniblog

Icon

Flame Artist by day. Proud Dad by, uhm, the rest of the time. Lover of everything Apple. Hater of everything Microsoft. Except for the Xbox 360 of course.

And Then There Were Three

Finally, the iPhone has more than one network in the UK. Two days ago Orange announced that they’ve broken O2′s stranglehold on the iPhone and will be selling all models by Christmas. Yesterday Vodafone declared they will be selling the iPhone as well from early 2010.

What will this mean to the iPhone? Considering that Apple recently reported that there are over 85,000 apps in the App Store and over 2 Billion downloads in its first year, those numbers will now skyrocket. There has never been a better time to start writing apps for the iPhone and the Mac platforms.

I’ve been dabbling with Xcode for a little while and found some really good resources to help me get started. The best guide so far has been BecomeAnXcoder and I highly recommend Aaron Hillegass’s book Cocoa Programming for Mac OS X. When you have a handle on Xcode and want to get started on iPhone development, there’s Stanford University’s excellent iPhone Application Programming podcasts.

Do it now if you can. The iPhone application market is about to expand massively.

First app: Count Characters

So SimpleWord is a little bit advanced at this stage. I started with one of the first challenges in Cocoa Programming for Mac OSX, a simple application that counts characters in a line.

The window should have a text field where the user enters a string of characters, a button which tells the application to count the characters and a label to display the result. I started with creating a new Xcode project called CountCharacters and in there I created a new class and called it AppController. In the header file AppController.h file I declared the following:

@interface AppController : NSObject {

IBOutlet NSTextField *input;
IBOutlet NSTextField *output;
}

- (IBAction)countIt:(id)sender;

@end

The first line creates an instance of NSObject and declares two outlets: input and output. When the user clicks the button, it triggers an IBAction method called countIt. With that done, I created a simple UI in Interface Builder:

Then it was time to make the connections in Interface Builder. I created a new Object in IB, called it AppController and assigned it the same class. I set the input and output outlets to the new object and set the action of the button to the clickIt: method.

Back in Xcode, here’s what I coded in the implementation file AppController.m:

#import “AppController.h”

@implementation AppController

// Set the output text label to ??? on first run
- (void)awakeFromNib
{
[output setStringValue:@"???"];
}

- (IBAction)countIt:(id)sender
{
NSString *string = [input stringValue]; // Get the value of input
NSInteger count = [string length]; // Get the length of string

NSString *display = [NSString stringWithFormat:@"'%@' has %i characters.", string, count];

[output setStringValue:display];

}

First, awakeFromNib changes the Label field in the window to ??? when the application first runs. The countIt method was simple to put together despite stumbling on some syntax. The string instance variable, or ivar, gets the value from the input field in the application and the count ivar returns the length. The display ivar is formatted with the resulting output. The last line changes the label in the window from ??? to the value of display. Here’s what it tooks like:

Beginning with Xcode

For the past few months, I’ve been dabbling with programming on the Mac but I haven’t had the time to really dive in. I was trying to explain to someone how it worked and realised that I couldn’t really articulate how. Therefore, this will be a series of posts on how I intend to build applications using Xcode, Apple’s free development environment.

There are plenty of excellent guides but I found that they all had one flaw: obsolescence. I would be following a guide until it reaches a point where the window, method or button isn’t there in my version of Xcode. And I come to a screeching halt. Lots of the guides have been written for previous versions of Xcode and haven’t been updated.

Again, I intend to document my journey through development on my Mac and iPhone. I’ll start with writing a simple word processor, SimpleWord.

Hi. I'm Hani and this is my blog. I also have some photos on Flickr, bookmarks on Delicious, tweets on Twitter and generalities on Facebook as well. Most of the time I can be found at Prime Focus in London, crafting commericals using Flame.

Conversationalists