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:
|
1 2 3 4 |
- (void)moveBall:(Ball *)roundBall
toPosition:(double)pos {
// do stuff
} |
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:
|
1 2 3 4 |
public function joinNames($firstName, $lastName) {
// do stuff
return $fullName;
} |
Simple enough: joinNames() takes two arguments, $firstName and $lastName, and returns $fullName.
Now here’s the Objective C equivalent:
|
1 2 3 4 5 |
-(NSString *)joinFirstName:(NSString *)firstName
withLastName:(NSString *)lastName {
//do stuff
return fullName;
} |
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:
|
1 |
$myClass->joinNames('John', 'Smith'); |
But in Objective C you would do something like this:
|
1 |
[self joinFirstName:@"John" withLastName:@"Smith"] |
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:
- Declare the method
- 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:
|
1 2 |
-(NSString *)joinFirstName:(NSString *)firstName
withLastName:(NSString *)lastName; |
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:
|
1 2 3 4 5 |
-(NSString *)joinFirstName:(NSString *)firstName
withLastName:(NSString *)lastName {
//do stuff
return fullName;
} |
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:
|
1 2 3 4 |
@interface MyClass()
-(NSString *)joinFirstName:(NSString *)firstName
withLastName:(NSString *)lastName;
@end |
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
-
TomLongo
