
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:

Conversationalists