Getting used to Objective-C Syntax
My previous experience with programming is mostly with languages similar to C in syntax. So when I started to learn Objective-C, while it is still a subset of C, it diverged in a much different fashion to its brethren. To give a little history lesson, Objective-C was began in the 1980s, as basically a combination of the languages “C” and “Smalltalk“. Other C-derived languages include C++ (obviously) and Java. Other Smalltalk-derived languages include Python and Ruby. Since I am not particularly versed in Python or Ruby though, the aspects borrowed from that side of Objective-C’s family tree are currently a bit of a mystery to me, hence my trying to pass on what I’ve learned about the syntax.
Using methods in C
So, in C based languages you usually create a method like this:
int someMathFunction(int length, int width);
And call it like this:
someMathFunction(5,10);
Simple enough, though one issue when you’re calling it, you can’t see what the parameters are at a glance. If you want to see them, you have to look up the prototype of the method to know what each of the parameters are. Unless you named the variables really well in the code where you called the function, this can be rather hard to understand at a glance. Some IDEs assist with this by showing the prototype when you hover over a method call, or see it in code completion, but it isn’t in the actually written code.
Using methods in Objective-C
In Objective-C, you would create the same method like this:
-(int) someMathFunctionWithLength:(int)length width:(int)width;
And call it like this:
[self someMathFunctionWithLength:5 width:10];
There is definitely some explanation necessary here.
The Method Prototype
The “-” character at the begining of the function declaration says that this is an instance method. This means that each instance of whatever class this is in, would have its own version of this method. For example, if this was a method in a class that describes a cube, and it had an instance variable for height, and this method would return the volume of the cube based on the length and width put into this method, but the height stored as an instance variable, this is where you would use an instance method.
On the other hand, if this method was in a class that describes a square, and needs nothing from an instance of this class, you could make it a class method. In this case, you would just use a “+” instead of the “-“. These are similar (but not identical) to static methods in C++, Java, or C#.
The rest of the method prototype is the 2 parameters. Unlike a normal C method, the first parameter is usually used as the name of the function, followed by its variable type, and what the parameter will be referred to internal to that method. So for the first parameter, you would refer to the length variable just as “length” and not the “someMathFunctionWithLength” name.
The Method Call
Objective-C is a subset of C, so you can call methods similar to C if you really wanted, but that is not how it is meant to be done in Objective-C. The method call here is called a “message”. A message is what is written between the two square brackets. The “self” keyword refers to the class this method is in. In the more object oriented C-derived languages, like C++, Java, and C# there is keyword to refer to the class you are in, called “this”, and “self” basically is used the same way here.
Remember how I mentioned earlier that C method calls don’t really tell you what the parameters are without looking up the prototype? This is one way Objective-C is quite helpful. At a glance, you can tell that this math function wants a length, and a width, and you don’t need to look it up anywhere. This does tend to give Objective-C rather long method names, but that description can be quite helpful. The Xcode IDE has pretty good autocompletion, so the long method names are not too much of a hassle to write.
So, the rest of that call just shows you the name of the parameter, and after the colon is what you are sending as that parameter. The parameters can be primitives or objects. Each of these parameters is actually something called a “selector” in Objective-C. I will get into the what that means in another post, but for now, just think of theme as the parameters for the function.
Anyway, I hope this introduction to the wonderful world of square brackets has been helpful. I plan to make a few more of these primer posts to describe some early and foundational ideas of Objective-C, particularly the ones that I found interesting or hard to get my head around. Thank you for reading this blog post, and have a great day.