Diving into Xcode

New Project

Let’s use our Cat and Mouse classes to build our first iPhone app. Open Xcode and create a new project.

Create new Objective-C class files for Animal, Cat, and Mouse, and fill them in with the following code.

Header Source

You can now use these classes in your program.

This generates the following output:


The NSLog(…) function is used to print messages to the console. It is similar to println(…) in Processing and printf(…) in C/C++.

You can pass variables to NSLog using the same format specifiers as printf.

Specifier Description
%d, %i Signed Integer
%f Float
%s C String
%@ Cocoa Object responding to -description

Some specifiers accept formatting options.

Option Description
(width) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces.
0 If (width) is specified, pads the number with zeroes instead of spaces.
.precision The number of digits to be printed after the decimal point.

These are just a small list of available specifiers and options. You can find the complete list here.

User Interface

Once you’ve connected all your outlets and actions, your ViewController should look like this.

Header Source

Add some private properties to work with.

Respond to the actions by updating kitty‘s weight.

Refresh the view with kitty‘s new weight.

Don’t worry about NSString too much for now, but take notice that we’re building it using the same format as NSLog.

You can download the Xcode project for this lab here.

  1. printf. C++ Reference. Visited Sep 4 2012.

  2. Joseph V. Crawford Jr. Using Foundation’s NSLog Function. JosephCrawford.com. Visited Sep 4 2012.

Comments are closed.