What is a Method in Objective-C Anyway?

You can think of this post as the first in a series of ‘The bits that nobody else explains very well’… An explanation ‘cheat-sheet’ article if you will, for anyone who’s starting to dabble in iOS or Mac OS programming and wants to not only ‘know’ how to program, but who wants to ‘understand’ what they’re coding as well. Many tutorials will gloss over these basic explanations, dismissing them as second nature, or failing to understand how fundamentally important it is that their readers understand the concepts behind the code they are being taught.

To some more experienced developers, these explanations might seem too simplistic and in some cases, even ‘wrong’ technically. That doesn’t matter here, what matters is that the fundamental concepts are understood. This article will be part of a series and if there’s a particular item you’d like me to cover just let me know in the comments section, or by heading to the contact form! Now without further ado, lets talk about Methods in Objective-C.

Methods In Objective-C

Right, now hang on a second… You know what a method is right? No… of course not, why would you even be reading this article if you did? So let me explain… Programming is essentially a set of instructions for a computer to carry out (in this case a shiny, drool worthy, iPhone… but a computer nonetheless). These instructions need to be wrapped in some sort of container in order for the code you write to make any sort of sense at all. Here’s an example I like to use, it’s called the ‘Making a cup of tea’ example…

If you wanted a cup of tea, and someone else was in the room with you, what would you say? Well, let’s assume you’re uncharacteristically rude and dispense with the pleasantries, you would say “Make me a cup of tea“… What you wouldn’t say is, “Take the kettle, fill the kettle with water, turn the kettle on, wait for the kettle to boil, take a mug, put a tea bag in it, once the kettle has boiled pour water from the kettle into the mug, take a spoon, stir the tea, remove the teabag, add milk, maybe sugar, put the milk away, put the spoon in the sink, bring me the cup

When you say to somebody “Make me a cup of tea” they understand the process to make a cup of tea, because it has already been explained to them in detail (probably a long time ago, but that’s irrelevant). When you ask somebody to “Make me a cup of tea” they know they need to carry out a set of instructions in order to fulfil that process and provide the output (In this case, a refreshing beverage!)

Methods to a computer are a way of containing these instructions, so you might decide to write the method “Make me a cup of tea” and within that method, you would write the code instructing the computer exactly how it should do that. The fantastic benefit of this is that now the computer knows how to “Make a cup of tea” you can call that method from within other methods, to perform that set of instructions. Essentially, once you’ve told the computer how to “Make a cup of tea” once, you need never tell it again.

Now, to dwell on this particular example just one moment longer… Remember that big long list of instructions that you wouldn’t say to another person if you wanted them to make you a cup of tea, “fill the kettle with water, turn the kettle on, etc…”. In the same way that you would write the method “Make me a cup of tea”, you would also write the method “Fill the kettle with water” which contained precise instructions for how exactly it would ‘Fill the kettle with water’. Within your “Make me a cup of tea” method, you would then simply call your “Fill the kettle with water” method as part of that process.

If you can understand this fundamental principle then you are one step closer to becoming an elite programmer! Seriously though, this may seem simple to the experienced programmers out there, but no one really explains this stuff very well and it’s important.

 

So what does a Method look like in code then?

Well, I’m glad you asked… I just happen to have an example right here! Before I show it to you though, allow me to explain a little bit of jargon I might use from now on. A programming language is like any other language, it has lexicon (words) and syntax (rules for combining those words) and as with any other language, there are many people out there who will argue for many, many days (and nights… seriously) about the correct use of programming syntax / lexicon. What I will use in these articles is examples that I consider to be ‘best practice’, I’m sure there will be people who disagree with my examples, but they’re not writing this article now are they!

So, a traditional objective-C method looks a bit like this…

[cc lang=”objc”]

– (NSString*)getName {

NSString * name = @”Jeff”;

return name;

}
[/cc]

What on earth does this method do? Well… lets switch a few names around, and it should all become a bit clearer…

[cc lang=”objc”]

– (CupOfTea *)makeMeACupOfTea {

CupOfTea * tea = milk + mug + water;

return tea;

}
[/cc]

So let’s examine what we have here. The first line tells us two things about this method, firstly `- (CupOfTea *)` tells us the ‘output’ of this method, or in english… what this method will give to whoever or whatever calls it. Secondly it tell us what the name of the method is, in this case `makeMeACupOfTea`, now we can choose what we call our methods (exciting huh?) and don’t underestimate this process. As soon as you get any number of different methods in one place, it becomes very important for you ‘the programmer’ to be able to quickly remember and know what each of your methods are called.

Right, so we know that this method is called `makeMeACupOfTea` and that it will output a `CupOfTea *`, Wonderful! Don’t worry about what the `*` means right now, we’ll get to that in another article. So the rest of the method is within a pair of curly brackets (Yep, that is the technical term for them!) Everything that is within the `{ }` is the ‘doing’ code of that method. Here you will find the code that actually makes the cup of tea.

All we are doing in this functional code, is creating a new object (more on this in a future article) called `tea` of type `CupOfTea` which is (if you recall) the output of this method. Don’t worry about how we’re creating tea here, in this example it’s just a bit of pseudocode (code which explains a purpose but is not actually functional or valid code) to make my point. So to finish the method off, all we do is `return tea;` Easy (When you know how!) So, now you know what my `makeMeACupOfTea` see if you can work out what the ‘real life’ `getName` method does? (A bit of googling will tell you what an `NSString` object is… Well, you didn’t think I was going to do ALL the work for you , did you?

 

Taking methods to the next level…

So, hopefully now you understand what a method is, what it’s purpose is, and how to use them. There is one more thing… Inputs, or parameters as they’re more commonly known. So it’s all very well, we have these methods to return us lovely objects, but what if we want to use these objects, say in another method for example. Well, that’s where a parameter comes in… You can pass another object into your method as a parameter. Let’s go back to our old `makeMeACupOfTea` example to demonstrate this, so if we use a parameter to pass the method a `TeaBag *` the method would probably look something like this…

[cc lang=”objc”]

– (CupOfTea *)makeMeACupOfTeaWithThisTeabag:(Teabag *)myTeaBag {

CupOfTea * tea = myTeaBag + water + milk;

return tea;

}
[/cc]

So here you can see that we have extended our method, to pass into the method the teabag that we want the method to use, in order to make the tea (at this point the `water + milk` are still elements of pseudocode purely for explanation purposes). So breaking down the method again, you can see we have a colon `:` in the method name, followed by the object type of the parameter in brackets, in this case `(Teabag *)` followed by the name of the parameter that you will use to reference the object you are passing into the parameter within the ‘doing’ code (the bit in-between the curly brackets if you remember). So anytime you type `myTeaBag` within your method, the computer will know you are specifically referring to the `myTeaBag` that you have passed into the method as a parameter.

We can take this a step further, and actually pass in multiple parameters to a method. This is done like so…

[cc lang=”objc”]

– (CupOfTea *)makeMeACupOfTeaWithThisTeabag:(Teabag *)myTeaBag Water:(Water *)myWater Milk:(Milk *)myMilk {

CupOfTea * tea = myTeaBag + myWater + myMilk;

return tea;

}
[/cc]

So now what we’re doing, is telling the computer to make a `CupOfTea` with this teabag, this water, and this milk! The formatting is probably wrong in this blog post, but that’s never what this was about. I don’t care right now if you don’t know the ‘best practices’ or if you are able to write code in the most efficient and optimal way as possible (although that becomes important later on.) Today I will be happy if you just understand methods a little bit more, and are slightly more comfortable in how to use them and what exactly they are doing.

Once you grasp the concept of methods, you’re one step closer to writing the next ‘Angry Birds’. Seriously though, it’s an important step into the world of programming. Even though this article has focused on Objective-C, the fundamental principles are the same in most programming languages, the lexicon and syntax will be different but the concept will be the same. This has been quite a long post, so my suggestion would be to go away, digest it, write some methods, and come back if you need any further guidance. Practice, makes perfect… Oh and remember: The computer is following instructions, it will only ever do what you tell it to do (and it’s really very fussy about how it gets told!)

This is the first of a series of articles, and is really the first time I’ve sat down to try to write a guide like this. I would appreciate any and all feedback, positive and negative! Just leave a comment below. Likewise if you want clarification on anything I’ve said in this article, let me know in the comments and I’ll update the article with more information on your point. For now, thanks for reading and happy coding!

Author: Sam

iOS Developer & blogger

2 thoughts on “What is a Method in Objective-C Anyway?”

    1. Thanks for your feedback, I’m glad you found the tutorial useful. I have a series planned, but I am incredibly busy programming right now! Posts will continue as and when I get more time!

Leave a Reply

Your email address will not be published. Required fields are marked *