Objective-C Tutorial

Objective-C Programming Tutorial

Objective-C is a programming language used in iPhone app development. You will be spending most of your time working with this language for most programming tasks that you will encounter as you work through app development. This tutorial is meant as a guide for programmers who want to get started fast on the language.

Objective-C Objects

Objective-C is an object oriented programming language and you will be spending most of your time creating and working with objects. To define an object to work with you simply write out the name of the class and a name that will serve as the local pointer to the object:

	NSNumber *numberObject;

NSNumber is a commonly used class in Objective-C and here our object is named numberObject. The asterisk is used to indicate that the object name is a pointer.

Messaging Objects

 When you need an object to do something in Objective-C you must send it a message. In other programming languages you may be used to “calling methods” with syntax that looks something like this:

	alert.show();

Objective-C handles this task a little bit differently by using this idea of messages and we will refer to this process as sending a message instead of calling a method. To demonstrate this syntax let’s stick with the example of an alert that we started with above.

To give some context, an alert is a common user interface element that pops up with a message for a user to read. In programming languages like dot net or Java you will probably call a method like “show” to invoke the behavior of alert that shows the message. To do the same thing with this object in Objective-C you would write this:

	[alert show];

The first thing you will notice is that the object and method are surround by square brackets. This is the trademark Objective-C syntax that people always notice and are sometimes vexed by. Secondly, check out how the method name is separated from the object here. In this context, we would refer to this statement as sending the “show” message to the “alert” object.

Messaging with Parameters

Often you will need to send a message that includes one or more parameters. In other programming languages this could look something like this:

alert.show("A Funny Message");alert.show("A Funny Message", 3);


In Objective-C we can do a similar thing, but we also get to include descriptive prefixes (specified in the class definition) to help us with code documentation. For example, here is how we would send messages like the ones above:

	[alert show:@"I say this!"];
	[alert show:@"I say this!" thisManyTimes:3];

As you can see the syntax is a bit different and you get a little bit more information. This appears wordy and confusing to many new Objective-C programmers, but you will get used to this soon enough (I mean it!).

Instantiating Objects in Objective-C

Before you work with an object in Objective-C you will need to create an local instance of the object. This process is called instantiating and when you are using Objective-C on the iPhone you will need to allocate memory as well as create an object. To allocate memory you send a alloc message to the class itself (you can send messages to both objects and classes).

To create the object itself, you will use a constructor that is generally prefixed with the word “init” or simply init. Here is how you instantiate an object (from now on I will be using generalized descriptive words for the various components in the examples):

	myClass *myObject = [[myClass alloc] init];

Releasing Objects

When using Objective-C on the iPhone you must release objects that you have created using alloc, new or copy. This has to do with memory management which will be discussed in the next section. To release an object you simply send the “release” message to the object.

	[myObject release];

Here is the pattern that you will typically follow when using objects in Objective-C:

	//allocate memory and create object
	myClass *myObject = [[myClass alloc] init];
	//Use object
	[myObject doStuff];
	//Release object
	[myObject release];

Memory Management and Reference Counting

When you are working with Objective-C on the iPhone you need to manage memory manually. This is a very detailed and important topic and if memory management is not done correctly then it could lead to memory leaks or app crashes.

Managing iPhone memory is a simple system called “reference counting”. The idea is that the system will keep track of whether it needs to keep the memory for an object available based on the number of other components that indicate that they want the object to stick around. Each component gets to indicate its interest in the object by adding a reference (sometimes called retain) count to the object.

You may add to an object’s reference count by sending the “retain” message to an object. So, if 5 components are interested in myObject then myObject has a reference count of 5. The system will keep your object’s memory in place as long as the reference count is above 0. As components no longer need an object to stick around they will remove their interest in the object by sending a “release” message to the object.

Each time a release message is sent to an object it’s reference count goes down by 1. Once the reference count reaches 0 the system may destroy the object and re-use the memory at any time.

Basic Memory Management

While reference counting is a simple system, it does require a lot of attention to detail and being consistent in how you handle this is very helpful. The most important thing to remember to do is to release every object that you create with the “alloc” keyword.

Every alloc must be matched with a release.

The other thing you must do is to make sure to match every retain message that you send to an object to a release message. Once you are finished with an object be sure that the reference count is 0. If you do this then you can be assured that you will not be wasting memory.

Every retain must be matched with a release

Do not send messages to objects that have been completely released and have a retain count of 0. If you do this your app may crash; this is the most common and vexing problems in app development. Here is an example of a typical life cycle of an object and it’s reference count during each step:

	myClass *myObject = [[myClass alloc] init];
	//Reference Count: 1

	[myObject retain];
	//Reference Count: 2

	[myObject retain];
	//Reference Count: 3

	[myObject release];
	//Reference Count: 2

	[myObject release];
	//Reference Count: 1

	[myObject release];
	//Reference Count: 0

Once myObject reaches a reference count of 0 at the end the system will destroy the object and if you attempt to send a message to myObject your app will crash. Notice how each alloc and retain is matched to a release. If you do not match alloc, new and retain messages with corresponding release messages you will either leak or crash your app.

Strings with NSString

Let’s move on to some classes and objects that are used frequently in Objective-C. Many of these come from Foundation which provides foundational programming functionality (sort of like it sounds). Check out the header files that come with XCode to see everything you can do with Objective-C. It provides a pretty rich framework. Moving on to strings: in Objective-C the class you use to work with string is called NSString and it is used like other objects:

	NSString *myString = [[NSString alloc] initWithString:@"A String"];
	NSLog(myString);
	[myString release];

NOTE: Above we use NSLog to write messages out to the console. Here we simply create an instance of NSString using the typical pattern describe above (alloc, init & release). However, NSString comes with additional functions that do not require you to use alloc or a release message. The same thing could be accomplished with this:

	NSString *myString = @"A String";
	NSLog(myString);

NSString also has functions that will help you compose new strings by gluing other strings, numbers and objects together using the stringWithFormat function:

	NSString *lotsOfInsertedStuffString = [NSString stringWithFormat:@"I am adding this number: %i and this string:%@.", 45, myString];
	NSLog(lotsOfInsertedStuffString);

NSString comes with a rich feature set so be sure to look up the functions available to you in the header files.

Counting and Numbers in Objective-C

For the most part you may as well stick to using regular C style integers and doubles when doing math. Objective-C and C and often used together and working with numbers using int and double is much easier when doing math. You can use the Objective-C class NSNumber however when you need to style numbers in a particular way. You will also be using the C style for typical programming activities such as: looping, if-then logic, case statements, structs and functions. C Refresher for Integers and Doubles:

	int i = 3;
	NSLog(@"i = %i", i);
	double d = 3.4;
	NSLog(@"d = %f", d);
	double dPlusi = d + i;
	NSLog(@"d + i = %f", dPlusi);

Objective-C Arrays with NSMutableArray

Before we end let’s take a look at how to use the object oriented arrays that you have available from Foundation. Arrays are simply lists of objects and in Objective-C you can put anything into array and you can even mix and match objects. Here is how you generally use an array:

	//Instantiate an array
	NSMutableArray *myArray = [[NSMutableArray alloc] init];

	//Add elements to an array
	[myArray addObject:@"Element 1"];
	[myArray addObject:@"Element 2"];
	[myArray addObject:@"Element 3"]; 

	//Retrieve an object from an array
	NSLog([myArray objectAtIndex:0]); 

	//Retrieve the last object in an array
	NSLog([myArray lastObject]);

One powerful reason to use arrays is that you can leverage the for-each loop which is a way of repeating the same action on every object in the array: for (NSString *s in myArray) { NSLog(s); } Of course, you must release the array object when you are finished (sorry for beating it in like this but it’s important!)

	[myArray release];

That’s a Wrap!

That should be enough to get you started in your Objective-C programming career. There are many more advanced features to the language that you will eventually want to take on once you have a bit more practice. But, this will give you enough to get started with iPhone development since you will be using Objective-C with the Cocoa-Touch frameworks used to make most iPhone apps.

One thought on “Objective-C Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>