How to implement methods in Objective-C

This is Part 2 in a three-part series on Methods in Objective-C. If you want to learn more, check out the others:

  1. How to read (and write!) method declarations in Objective-C
  2. How to implement methods in Objective-C
  3. How to send messages (aka call methods) in Objective-C

OK, so you know how to declare methods in Objective-C, but do you know how to implement them so your method can actually do something? You’ll find implementing them to be pretty straightforward since you already know how to declare them. In fact, the opening syntax looks exactly the same. Let’s look at that driveCar method declaration we saw earlier:

- (void)driveCar;

And now, our implementation:

- (void)driveCar
{
    NSLog(@"Driving!");
}

And really, that’s it. In the implementation, we implement the driveCar method using the exact same syntax as the method declaration. In this case, it’s an instance method, as indicated by the minus sign (-). It returns void (nothing), and it’s called driveCar – exactly the same as the method declaration. And then there are some curly braces to surround the body of the method. Notice that opening curly braces ({) tend to get their own line in Objective-C, perhaps unlike what you’re used to. This is the way Apple does it in their sample code and the way I prefer, as I find it easier to match opening and closing braces when they’re vertically aligned. Inside of those, we have the code that runs when the method is called. Here we’re just calling the NSLog function to print “Driving!” to the console so we know we’re indeed driving that car. Pretty simple, right?

But where does all of this code go?

Well, the method declaration goes in an interface called CarController.h, like so:

@interface CarController : NSObject

- (void)driveCar;

@end

The @interface tells our trusty compiler that this is, in fact, an interface we’re about to declare. CarController is the name of our class, and the colon (:) following indicates that it extends NSObject. In other words, CarController is a direct subclass of NSObject – the root class for every class in Objective-C. (“Wait a minute,” you may be thinking, “does this mean I can’t subclass another class?” Of course you can – you can subclass any class you want. By “root class,” I just mean that if you trace any class far enough up the hierarchy, you’ll always end at NSObject.)

OK, so just below the @interface line, we have our method declaration, which you already know about. And then there’s the @end, signifying that yes, we’re quite finished defining this interface.

Onward and upward…to the implementation file, CarController.m, where our method implementation lives. (Notice how easy that is to remember? Implementation…implementation…)

#import "CarController.h" 

@implementation CarController

- (void)driveCar
{
    NSLog(@"Driving!");
}

@end

So in the implementation, we start by importing our CarController header file. That tells the compiler which methods need to be implemented in this implementation file. In our case, we’d better implement that driveCar method or we’ll get a bright yellow warning from the compiler in Xcode.

On the next line, the @implementation begins our implementation of, yes, the CarController. And in case you’re wondering, the @interface’s name and the @implementation’s name do need to match. (Otherwise, what are you implementing?) Following the @implementation line is the method declaration we already discussed, and then, as in the interface, the implementation ends with an @end.

Now you can declare methods and implement them in Objective-C, and you even know where to do it. (And in a rather fortunate coincidence, you also learned how to create classes.) Just imagine how much you’ll be able to accomplish once you know how to call those methods on your CarController – or, as we say in Objective-C, send messages to a CarController object.

Want to learn Swift, too? Drop your name & email in the boxes below to get the 5-Part Guide to Getting Started with Swift.