First time working with Objective-C?

Featured Image

Do you want to build nice-looking and good-working applications for iPhone®? Let’s start by assuming that you already have all the “man needs” to develop applications for the applications (registration, the newest iPhone SDK, Xcode running on your iMac, etc.) What now?

The first step in learning how to develop iPhone applications is learning Objective-C, an object-oriented programming language that we use to develop iPhone applications.

Objective-C extends the standard ANSI C language by providing syntax for defining classes, methods, and properties, as well as other constructs that promote dynamic extension of classes. The use of objects and object-oriented constructs is fundamental to the design of iPhone applications, and understanding how they interact is critical to creating your applications. I strongly suggest reading The Objective-C 2.0 Programming Language first, especially if you are just starting to build iPhone applications.

Here are a few essentials you will need when you are programming with Objective-C:

In Objective-C, object identifiers are a distinct data type: id. This type is defined as a pointer to an object. In reality, it is a pointer to the instance variables of the object, the object’s unique data. Like a C function or an array, an object is identified by its address. All objects, regardless of their instance variables or methods, are of type id.

id anObject;

The keyword nil is defined as a null object, an id with a value of 0. id, nil, and the other basic types of Objective-C are defined in the header file objc/objc.h.

Memory Management is very important. In an Objective-C program, it is important to ensure that objects are deallocated when they are no longer needed, otherwise your application’s memory footprint becomes larger than necessary. It is also important to ensure that you do not deallocate objects while they’re still being used.

Message Syntax: To get an object to do something, you send it a message telling it to apply a method. In Objective-C, message expressions are enclosed in brackets.

[receiver message];

The receiver is an object, and the message tells it what to do. In source code, the message is simply the name of a method and any arguments that are passed to it. When a message is sent, the runtime system selects the appropriate method from the receiver’s repertoire and invokes it.

For example, this message tells the myRectangle object to perform its display method, which causes the rectangle to display itself:

[myRectangle display];

Dot Syntax (syntactic sugar): Objective-C provides a dot (.) operator that offers a compact and convenient syntax you can use as an alternative to square bracket notation ([ ]) to invoke accessor methods. It is particularly useful when you want to access or modify a property that is a property of another object (that is a property of another object, and so on).

myInstance.value = 10;
printf(“myInstance value: %d”, myInstance.value);

is identical to

[myInstance setValue:10];
printf(“myInstance value: %d”, [myInstance value]);

Creating Instances: As you know, a principal function of a class object is to create new instances. The alloc method dynamically allocates memory for the new object’s instance variables and initializes them all to 0—all, that is, except the isa variable that connects the new instance to its class. For an object to be useful, it generally needs to be more completely initialized. That’s the function of an init method. Initialization typically follows immediately after allocation:

id myRectangle;
myRectangle = [[Rectangle alloc] init];

And that will be all for now.

1 comment

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=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.