iOS Newbie Part 5 – Method Man

03 Jul

Hopefully by now you’re forming an understanding of what objective-c classes are.  Time to concentrate on how you get classes to do things.  The only way to get a class (or class instance) to do something is by sending it a message.

You’ve seen in the previous parts of this tutorial how to create an instance of the Animal class.  Just like this:

The green highlight above shows the Animal class being sent the message ‘new’.  A message is a call to an existing method named ‘new’ within the Animal class.  The ‘new’ method allocates memory for and initializes a new instance of the Animal class.  Of course, from now on we can to this class instance as ‘mySecondPet’.

In other programming languages you might call a method a function.  Methods are used to logically separate different pieces of code within a class.  Each method is enclosed in {curly braces}.

Creating a Method

Lets create a new method to get our Animal to do a trick.  There are two things you need to do.   The first is to simply add a new method to the Animal.m file after the init method finishes.

- (void)doTrick {
CCLOG(@"Doing a trick");

Note the minus sign in front of the (void) indicates that this is an instance method and not a class method.  Don’t worry about that for the moment, what I want you to focus on is the (void) bit itself.

The stuff in the brackets indicates whether or not this method returns a value.  If it says void, it doesn’t return any value.  If it does return a value then there will be a variable type listed instead.  Methods can return any valid variable type you specify.

The text on the same line after -(void) is just the method name, in our case ‘doTrick’.  You could specify some input variables after the method name, more on that later.

The next thing you have to do whenever you create a method you want to access outside of the class is to pop a line in the interface file (.h) to let the world know that they can use this method.

Go ahead and add the following on a new line after the @property declarations
in Animal.h :

- (void)doTrick;

Once you have the interface (.h) and implementation (.m) set up you can tell an instance of Animal to doTrick!

Go back to the and type in [myFirstPet doTrick]; on a line before [myFirstPet release];.  If you write it on the line after you release the myFirstPet class instance then you’re in for trouble because myFirstPet won’t exist anymore.

You should now have the following code and the result I’ve highlighted in green below:

Easy huh.

Passing Variables to Methods

What if I wanted myFirstPet to perform a specific trick? like trick 7 in from it’s box of tricks?  How would I relay that information to the doTrick method?  What we need is a way to pass a variable to the doTrick method!  Here’s how:

The green highlighted bit above is the variable type of the variable highlighted in orange.  You can use this variable name within the method as demonstrated.

Now we have updated the method in Animal.m we need to ensure the header (Animal.h) is updated so we can still use it outside the class.  From the .m I just copy the whole method name up to the  curly brace { and then paste it into the header file with a ; symbol after it.

Jump back to and find [myFirstPet doTrick] again.  You’ve probably got a warning saying ‘instance method not found’.  That’s because the method now takes a new variable and you must specify what it is.  Backspace some of that code and then press ESC on your keyboard.  You should get a pop-up list suggesting potential completions to the line of code.  It’s pretty useful.

Press tab with the pop-up list up and then type in the number 7 as the integer.  Add a ]; on the end and your code should look like mine shown below:

When you run the app you’ll now have a log entry saying that trick 7 was performed.  Try some tricky stuff now, perhaps you could declare an int called selectedTrick with a value of 8 and pass that to the new method instead of just passing 7.

Passing Multiple Variables to Methods

Before I wrap up this part of the tutorial I should cover how to set up methods to receive multiple variables.  Lets see if one picture can explain it:

The blue highlighted bits are the variable names that can be seen outside the method.  When you’re sending a message to this method and press esc these are what you can see as descriptors to the variables being asked for.

The green highlights show the variable type.

The orange highlights show the variable names that can be used within the method.

That just about wraps up part 5.  I’ll proof read tomorrow and publish early just for Adam (hi mate /wave)

If you liked this tutorial or found something wrong with it please let me know!

If you want to support my work and have an iPad please consider purchasing iSoccer *wink*


Go to Part 6 or visit Tutorials Index

Be Sociable, Share!

    About Tim Roadley

    I'm a Technical Solutions Architect at Park Assist (TKH Group). My current focus is the design and implementation of a Network Operations Centre (NOC) for Westfield (Scentre Group). Prior experience includes successful design and implementation of custom business intelligence dashboards for Westpac and a payments switch for Cuscal (RediATM). My current skill set includes solution architecture, business analysis, stakeholder management, project management, consulting, software development, IT infrastructure management and technical documentation. I've published a book on Core Data through Pearson Education, which you can find on Amazon by searching for my name. An updated version on this book is targeted for release before the end of 2016. I have several apps on Apple's App Store, including Teamwork, iSoccer, Grocery Dude (Objective-C) and now Groceries (Swift). In my down time I enjoy spending time with my wonderful wife Tracey and two lovely children Tyler and Taliah.

    Posted by on July 3, 2011 in iOS Tutorials


    6 Responses to iOS Newbie Part 5 – Method Man

    1. Alexandre Laplante

      March 24, 2012 at 5:13 am

      In the doTrick methode i cant use the CCLOG, xcode tell me “Implicit declaration of function ‘CCLOG’ is invalid in C99…

      • Yup

        March 26, 2012 at 6:01 pm

        Yup I got the same problem, seems like we have to import some clases?

      • Mirqobil Mirpayziev

        April 2, 2012 at 10:16 pm

        CCLog is Cocos2d macro for the NSLog method.. try to use NSLog

    2. PS

      November 20, 2012 at 1:24 pm

      great tutorial! but i cant get it to work since part3, when i run it says build failed, running on xcode4.5.2 :/

    3. Gusty

      October 2, 2013 at 12:42 am

      Are the blue highlighted bits actually the method names?


    Leave a Reply

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