posted by Jared White on Thu 24th Apr 2003 17:49 UTC

"Cocoa 101, Part 1, Page 3"

The Cocoa framework, while being very well organized, is very large and complicated, so I'll save a lengthy overview of it for Part 2 of this article. But I'd like to get you up to speed on some of the very basics of the Objective-C language. I'm not going to get much into all of the philosophy and design principles of object-oriented programming right now. For that, I heartily recommend you read Apple's excellent online book The Objective-C Programming Language. To sum it up, objects are nifty. Objects allow for "user-oriented" program design. Instead of designing the program's architecture based on some arbitrary code organization only you will understand, you can design it around the intended feature set of the application. For instance, you can create a Person object, subclass it (more on that later) to make a JaredWhite object, and send it a writeCocoaArticle message. You can then tell it to notify you when the article is done, and when you receive its notification message, you can then send it a sendCocoaArticletoOSNews message and receive a yes or a no to determine if the transmission was successful. Objects also make reusing code easy, since an object only handles a specific, obvious portion of your application code, and in many cases is general purpose rather than specialized.

Now let's down and dirty and start looking at code. Objective-C messages look like this (remember, in C, every statement ends with a semi-colon):

[anObject doSomething];

This line of code is telling "anObject" to "doSomething". Or, more specifically, anObject is the receiver of the message "doSomething". All messages in Objective-C are always enclosed in square brackets. This looks very different from C or C++, but it's a simple, easy-to-read format once you get used to it. doSomething is the name of a corresponding method in the object, and is implemented like this:

- (void)doSomething {
    // some code

This method doesn't return anything, so its return value is of the "no value" C date type void. All the method's code is contained within the braces, just like with a regular C function. The two forward slashes indicate that all the remaining text on the line is a comment, not code meant to be complied.

The minus sign before (void) indicates that this method is an "instance" method. If it were a plus, it would indicate that it is a class method. In Objective-C, each object belongs to a class. A class is a "blueprint" for object instances, and is actually initialized as an object itself by the Objective-C runtime. You can send this class object messages, but the only methods available are class methods as mentioned above. Basically, you can create as many instances of a class as you like, but there's only one class object initialized by the Objective-C run-time ever during the execution of your app (so you don't have to worry about it). In fact, you use a class object to create an instance of an object. The most common line of code to accomplish that is:

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

This code looks complicated, but it's really very simple. The bit before the equals (=) sign says that the myObject pointer (remember, a pointer is a variable pointing to an object stored in memory, and you put a * before the variable name when you first "declare" it to indicate that) is going to be pointing to an object belonging to the MyKindOfObject class. The bit after the equals sign is a group of nested messages. Basically the way it works is that you can send a message to the object returned by a message. So the object returned by [MyKindOfObject alloc] is sent an init message. The alloc message calls the standard Cocoa alloc class method (also called factory method, since class objects can also be called factory objects because of their use as object instance creators). If you had written it yourself, you would have written it like this:

+ (id)alloc
    // code to allocate memory and return an uninitialized object

Compare that method to the doSomething method above. This one has a plus sign, since it's a class method and not an instance method, and it's return value is an id, which means that it is a pointer to the object that was just created (note that you never put a * before id, because the fact that id is a pointer is already part of the data type).

Now back to that earlier message above. Outside of the [MyKindOfObject alloc] message, we see an [... init] message. Because alloc returns an uninitialized object instance, we have to send that object an init message to initialize it. init is a standard Cocoa instance method.

Once an object is initialized, it can be used for anything you like. Until then, it's of no use to anyone. So remember, you first have to create an object by telling the class object to allocate the memory for a new object and return a pointer to that object, and then you have to tell that object to initialize itself. Although there are more ways to code that procedure besides the one I presented above (more on that next time), you should always keep this concept in mind.

Table of contents
  1. "Cocoa 101, Part 1, Page 1"
  2. "Cocoa 101, Part 1, Page 2"
  3. "Cocoa 101, Part 1, Page 3"
  4. "Cocoa 101, Part 1, Page 4"
e p (0)    56 Comment(s)

Technology White Papers

See More