iPhone Development 101

iPhone 101

Code Tips

Resources

Links

Subscribe

TwitterRSSE-mail

More

Learn iPhone Programming:
Intro to Objective-C: Classes

This tutorial was written for Xcode 3.2; it's outdated now. For a more up-to-date tutorial, check out Apple's App Development Tutorial to learn how to write iOS apps. There are also lots of other excellent books and online tutorials you can learn from.

I'll still be posting code tips and shortcuts to idev101; click here for the index. If you'd like to get updates, follow me on Twitter, or by RSS or E-mail.

First we'll take a look at the programming language that iPhone apps are written in: Objective-C. You'll get some experience writing code as this course progresses, but for now this is just a quick overview so you'll have a rough idea of what you're looking at when you write code. For an in-depth course on the Objective-C language, pick up a copy of Programming in Objective-C 2.0.

Objective-C is an extension of the C programming language. It's an object-oriented language, with some extended syntax that's a bit different than most other languages.

Cocoa is the name of the set of frameworks (or code libraries) that Apple provides for developing Mac programs. Similarly, Cocoa Touch is the set of frameworks for developing iPhone apps.

Classes

A Class is is a collection of properties (values) and methods (code) that handles a specific task. For example, the UIWindow class contains properties and methods relevant to window objects on the iPhone. NSString is a general string-handling class. You'll be using existing classes and writing your own classes as you develop apps.

A class is usually separated into two files: the interface or header file (.h) and the implementation file (.m):

.h file
The Class @interface
.m file
The Class @implementation
This is where you:
  • Import headers from other classes
  • Declare Variables
  • Declare Methods
This is where you write your code.
  • Implement the code for each method declared in the .h file.
  • Customize/override methods from a parent class
  • Implement any required delegate methods.

The Class @interface

The @interface (or .h file) tells the compiler the name and parent of your class, and declares any public variables, properties and methods. Here's what it looks like:

#import <UIKit/UIKit.h>         // import required headers

@interface ClassName : ParentName <protocols> 
{
    UIWindow *window;           // declare an instance of UIWindow
    int count;                  // declare an integer variable named "count"
}

// Instance variables you declared above can be made into properties for key-value coding:
@property (nonatomic, retain) IBOutlet UIWindow *window;

- (void)helloWorld;             // declare a method named helloWorld

@end

The first line, #import <UIKit/UIKit.h>, imports the UIKit framework header file. It's required for all iPhone apps.

The @interface line tells the compiler the name of your class and its parent class. It also lists any protocols the class conforms to (if any).

Variables must be declared inside the { curly braces }. When you declare an instance of an Objective-C Class, the instance name must be prefixed by an asterisk, which indicates that this is a pointer to the memory space for the object. It doesn't matter if you put spaces around it. Any of these is valid:

    UIWindow *window;
    UIWindow * window;
    UIWindow* window;

Variables of C types (such as int, float and struct) and don't need the asterisk.

Next you have @property declarations. (These go AFTER the curly braces are closed.) Properties are optional, but by declaring your instance variables as @properties, you can use dot-notation (such as self.window) to refer to those values.

All @property variables must be @synthesized in the implementation file.

Finally, if your class has any custom methods, they should be declared before the closing @end line.

The Class @implementation

The implementation (.m) file is where you'll write your code. It typically looks like this:

#import "ClassName.h"           // import the header file for this class

@implementation ClassName

@synthesize window;             // all @properties must be @synthesized

- (id)init {    
    [super init];
    return self;
}

- (void)helloWorld {            // implement the method declared in the .h file
    // your code here
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

You must implement any methods you declared in the @interface file. (In this example that's the helloWorld method.) You also must implement any required methods for any protocols this class adopts.

You may implement (or override) any method from a parent class. You'll often be adding the init and dealloc methods to customize the object's initialization and destruction.

Additional References


TopHomeContents