Home > Articles > Programming > General Programming/Other Languages

Cocoa Programming for Mac OS X: Objective-C

  • Print
  • + Share This
Cocoa was developed using Objective-C, and most Cocoa programming is done in Objective-C. Teaching C and basic object-oriented concepts could consume an entire book. This chapter assumes that you already know a little C and something about objects and introduces you to the basics of Objective-C.
This chapter is from the book

Once upon a time, a man named Brad Cox decided that it was time for the world to move toward a more modular programming style. C was a popular and powerful language. Smalltalk was an elegant untyped object-oriented language. Starting with C, Brad Cox added Smalltalk-like classes and message-sending mechanisms. He called the result Objective-C. Objective-C is a very simple extension of the C language. In fact, it was originally just a C preprocessor and a library.

Objective-C is not a proprietary language. Rather, it is an open standard that has been included in the Free Software Foundation’s GNU C compiler (gcc) for many years. More recently, Apple has become heavily involved in the clang/LLVM (Low Level Virtual Machine) open source compiler projects, which are much faster and more versatile than gcc. In Xcode projects, LLVM is the default compiler.

Cocoa was developed using Objective-C, and most Cocoa programming is done in Objective-C. Teaching C and basic object-oriented concepts could consume an entire book. This chapter assumes that you already know a little C and something about objects and introduces you to the basics of Objective-C. If you fit the profile, you will find learning Objective-C to be easy. If you do not, our own Objective-C Programming: The Big Nerd Ranch Guide or Apple’s The Objective-C Language offer more gentle introductions.

Creating and Using Instances

Chapter 1 mentioned that classes are used to create objects, that the objects have methods, and that you can send messages to the objects to trigger these methods. In this section, you will learn how to create an object and send messages to it.

As an example, we will use the class NSMutableArray. You can create a new instance of NSMutableArray by sending the message alloc to the NSMutableArray class like this:

[NSMutableArray alloc];

This method returns a pointer to the space that was allocated for the object. You could hold onto that pointer in a variable like this:

NSMutableArray *foo;
foo = [NSMutableArray alloc];

While working with Objective-C, it is important to remember that foo is just a pointer. In this case, it points to an object.

Before using the object that foo points to, you would need to make sure that it is fully initialized. The init method will handle this task, so you might write code like this:

NSMutableArray *foo;
foo = [NSMutableArray alloc];
[foo init];

Take a long look at the last line; it sends the message init to the object that foo points to. We would say, “foo is the receiver of the message init.” Note that a message send consists of a receiver (the object foo points to) and a message (init) wrapped in brackets. You can also send messages to classes, as demonstrated by sending the message alloc to the class NSMutableArray.

The method init returns the newly initialized object. As a consequence, you will always nest the message sends like this:

NSMutableArray *foo;
foo = [[NSMutableArray alloc] init];

What about destroying the object when we no longer need it? We will talk about this in the next chapter.

Some methods take arguments. If a method takes an argument, the method name (called a selector) will end with a colon. For example, to add objects to the end of the array, you use the addObject: method (assume that bar is a pointer to another object):

[foo addObject:bar];

If you have multiple arguments, the selector will have multiple parts. For example, to add an object at a particular index, you could use the following:

[foo insertObject:bar atIndex:5];

Note that insertObject:atIndex: is one selector, not two. It will trigger one method with two arguments. This outcome seems strange to most C and Java programmers but should be familiar to Smalltalk programmers. The syntax also makes your code easier to read. For example, it is not uncommon to see a C++ method call like this:

if (x.intersectsArc(35.0, 19.0, 23.0, 90.0, 120.0))

It is much easier to guess the meaning of the following code:

if ([x intersectsArcWithRadius:35.0
                   centeredAtX:19.0
                             Y:23.0
                     fromAngle:90.0
                       toAngle:120.0])

If it seems odd right now, just use it for a while. Most programmers grow to appreciate the Objective-C messaging syntax.

You are now at a point where you can read simple Objective-C code, so it is time to write a program that will create an instance of NSMutableArray and fill it with ten instances of NSNumber.

  • + Share This
  • 🔖 Save To Your Account