Objective C Method Syntax

Day 2

As a PHP developer, when I first saw the syntax for an Objective C method I just about gave up then and there. In fact, I didn’t even know at the time I was looking at a method. Now that I understand it, I can appreciate its elegance, but boy is it funny looking.

Here is an example of an Objective C method:

What is going on there?! 

Anatomy of a Method

I think the best way to explain how these work is to show you the Objective C equivalent of a PHP method.

Take this PHP method:

Simple enough: joinNames() takes two arguments, $firstName and $lastName, and returns $fullName.

Now here’s the Objective C equivalent:

OK, let’s work our way through the code:

  • (NSString *)
    This first bit is indicating what this method is going to return (in this case an NSString). If the value of fullName is not an NSString, or if you don’t return anything, you will get an error. The * after NSString states you want to create a pointer to the NSString object. If this method should not return anything, then you would change (NSString *) to be (void).
  • joinFirstName:(NSString *)firstName
    This is the first argument. Unlike other languages, the name of the method is generated by the arguments themselves. getFirstName is the keyword for the argument, and firstName is the argument name. firstName is what you will use in the method’s code to refer to the value given.
  • withLastName:(NSString *)lastName
    This is the second argument. lastName will be the value provided and can be used in the method much like $lastName in the PHP code example.
  • return fullName
    Obviously fullName would need to be defined in the method at some point, but once it has been created it is returned in the same way you would be used to from PHP.

How do I call the method?

So how do you call this method? In something like PHP, if you wanted to call a method you would do something like:

But in Objective C you would do something like this:

The square brackets really threw me off at the start, but is something you’ll get used to over time. Here’s the breakdown of everything within the brackets:

  • self
    This is the class where you will find this method. Self is the current class in which this code exists.
  • joinFirstName:@”John”
    After a space we define the  first keyword in the method name (joinFirstName) similar to before, but this time we give the value @”John” after the colon . The @ sign before the ” is a shorthand to define this argument type as NSString.
  • withLastName:@”Smith”
    As with the first name, the last name keyword and value is defined here.

As unfamiliar as this syntax is, you can see there is some elegance to its structure. As you read through the line there is a logical flow, you can read the line as:

“join the first name John with the last name Smith”

Also, by declaring that the method will return an NSString and accepts two NSStrings as arguments, we make it clear to anyone looking at our code exactly what should happen. It also helps Xcode give you meaningful errors when it tries to compile.

Putting it all Together

So that’s a method, but to put it all together we need to do a couple of things:

  1. Declare the method
  2. Implement the method

By “Declare the method” I mean we need to state what methods the current class contains. By “Implement the method” I mean simply write the code for the method (which is what we did above).

In PHP and other less strict languages, you just need to implement the  method, but in Objective C you need to declare it before you implement.

To declare the method, you need to add it to the header file, like so:

Notice code is exactly the same as our implementation, but without the {} or the code. Once we’ve declared it in our .h file, we can implement it in our .m file:

It should be mentioned that by declaring your method in the .h file you are making it public. If you want to make it private you should remove the declaration from the .h file and add this to the top of your .m file:

So that’s it! That’s an Objective C method.

Even once I got my head around the syntax it still feels a little weird, but I’m sure over time it will become second nature.

  • Phogue
    Given the similarity to C++ I would have expected a visibility modifier within the header. In C++ you can change the visibility of the method defined in your .cpp (.m equivalent) by using private, protected, public over sections of code within your header. I'd figure the equivalent php function would be something like $myClass->joinFirstNameWithLastName('John', 'Smith');  It's odd looking at the objective-c method call and both liking and disliking it at the same time :) Don't think I'm a fan of having to reference the class name when using self, since I would expect self to be the instantiated class. Would [[self] joinFirstName:@"John" withLastName:@"Smith"] work in a similar way, or does referencing the class name allow for additional implemented interfaces with identical method signatures? If that's not the case, then I think its funny looking.. if it is the case then again I like it but don't like it :)
    • TomLongo
      Hey Phogue! My example was a little misleading. You're right -- if the method joinFirstName:withLastName was within the class I was calling it from I would simply write [self joinFirstName:@"John" withLastName:@"Smith"] as you suggested.  In my example, myClass would need to be a property of self that contains the method joinFirstName:withLastName.  I might change the example for clarity.