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:

NSLog

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.

Introduction to Objective-C

  • High-level, object-oriented programming language.
  • Main programming language used by Apple for OS X and iOS.
  • Strict superset of C. It is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class.[1]

Properties

  • Instance variables are not accessed directly, they are accessed using properties.
  • A property is the combination of a getter and setter in a class:
    • The getter’s name is the name of the property:
      someValue
    • The setter’s name is the capitalized name of the property prefixed by “set”:
      setSomeValue:

Syntax

All classes inherit from NSObject.

Header Source

Here is the traditional, uncool way to create a private variable with a getter and setter:

Header Source

Here is the better way, using a synthesized property.

Header Source

This automatically creates the weight getter and the setWeight: setter.

Header Source

You can manually name a getter and/or setter, but that’s usually a bad idea as it breaks convention.

Header Source

You can make a property readonly, in which case it won’t have a setter.

Header Source

The Cat and Mouse classes extend Animal, and they inherit its public properties and methods.

Header Source

  1. Objective-C. Wikipedia, Aug 27 2012.

Model-View-Controller

MVC is a software design pattern that decomposes an application into three parts: the Model, the View, and the Controller. MVC is the strategy of choice in Cocoa, and is particularly good practice for GUI applications.

Model The data, and the rules that govern reading and writing this data.
View The presentation of the data.
Controller The logic that makes it all happen.

Communication

  • Controllers can always talk directly to their Model and to their View.
  • The Model and View should never speak to each other.
  • The View communicate with the Controller "blindly”:
    • The Controller drops a target onto itself.
    • The Controller hands out an action to the View.
    • The View sends the action when something happens in the UI.
  • The View does not “own” the data that it displays, it acquires it from the Controller, which interprets/formats Model data for that specific View.
  • If the Model has data to update, it broadcasts this information, and the Controller can tune into this broadcast.

Don’t worry if this is confusing for now, it will make more sense once we start working with it concretely. Just remember that the Model and the View should never communicate directly.


  1. Paul Hegarty. Lecture 1 Slides. iPad and iPhone Application Development. Stanford, Nov 14 2011.

  2. Model-view-controller. Wikipedia, Aug 30 2012.

  3. Nadir Gulzar. Fast Track to Struts: What it Does and How. TheServerSide.com, Nov 1 2002.