Blog Archives

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!

Tagged with: , , , ,
Posted in Informative

Thoughts on Android (from an iOS developer)

Google Nexus 7 Tablet On Wooden Desk

So, those who know me, will know me as an avid Apple fanboy through and through… But you know what? Okay, yeah they’re absolutely right. I love my Apple gear for its robustness, and its reliability. But recently, i’ve been becoming more and more frustrated with iOS. Specifically from a developer point of view, there are things I want to do, Apps i want to create that I just can’t because of Apples strict enforcement of the App store rules.

Now i’m aware that these rules supposedly lead to a lovely clean App store full of apps which all ‘just work’ etc, but in reality there are a ton of Apps out there that are utterly useless but have been ‘approved’ by Apple for sale. I’ll give you an example of an App I want on iOS, but can’t have (without jailbreaking of course!). An app that scans the air for all the WiFi signals in the area, tells me which channel / frequency is the best one to use for my wireless router for minimum interference. I also want to be able to monitor signal levels (in dB) around my house and make sure i get a strong signal everywhere. Basically that’s because i’m a massive geek really cool guy, but also just a little bit because Apple won’t let me!

[caption id="attachment_263" align="alignright" width="300"]Google Nexus 7 Tablet On Wooden Desk Google’s Nexus 7, a small tablet with some impressive specs. A quad core processor, 1 Gig of ram all for less than £200[/caption]

Android on the other hand… Well there’s no problem with android. I could make an app which did that, stick it up on the Google play store and have it ready for download in a couple of days if i really wanted to! My problem with Android, it’s not with Google and it’s nothing to do with the Core OS, rather it’s the crap that manufacturers and mobile carriers love to smother over the top of a perfectly nice clean operating system. HTC’s Sense UI is a perfect example, in my eyes it’s ugly, it’s dressing up android as a consumer level OS, when in reality it’s screaming out to be the OS of choice for geeks around the world. In some ways, efforts like HTC’s Sense is to Android, what OS X is to Unix (but not in a good way). It delays updates, performance improvements, and more… All for a (so called) pretty UI which consumers are supposed to like.

But here’s the thing, Google have had their line of Nexus devices for a fair while now, and what they provide is the very core of Android, the ‘vanilla’ kernel if you like. With no funky magical animated unicorn home screen backgrounds (although I’m sure you can enable that if you really must!) and finally with Jellybean, a system which is comparable (in my eyes) to iOS. Google have FINALLY sorted out the ‘Android Lag’ between touching the screen and stuff actually happening, and what’s more they have introduced a 7″ tablet, for under £200 that actually has some really nice specs. It really looks like a dream device for an App developer, which probably explains why i’m so very tempted to get my hands on one, and give Android development a go.

Couple this with the strange goings on at Apple of late, I’m talking about things like the sudden withdrawal of all machines from EPEAT only to have the decision reversed days later, in addition to the plain ugly leaks of what is rumoured to be the next iPhone. Apple appears to be faltering. I hate to say it, but i really think Apple is going to get some serious competition in the next year or two, from the likes of Google especially, but also Microsoft who appears to be trying to drag itself back into this century with PC+ and it’s Surface offering (supposedly a tablet, with no price or release date… We’ll see Microsoft, we’ll see!). Either way, it’s going to be an interesting couple of years, and it’s about time Apple got some serious competition… Now without further ado, i’m off to buy a Nexus 7, but don’t you think for one second that means i’m going to get rid of my iPad…

Let me know what you think, Android vs Apple… Will Microsoft even get a say in the tablet market, oh and does anybody remember the blackberries?

Tagged with: , , , ,
Posted in Thoughts