iOS Newbie Part 3 – OMG Theory

29 Jun

At the end of part 2 the log window was introduced.  By the end of this next part of the tutorial you’ll be logging information to the log window, which is pretty much a requirement no matter what app you’re making.  First we need to look at some of the building blocks of Objective-C code.  I’m not going to introduce you to everything, I’m going to skip over heaps of stuff.  What I will do however is focus on some areas you must understand.  At least this way you won’t have wasted your time learning something you rarely or never use.  Forgive me if I cover things you already know, the target audience will range in skill level.

Common Variable Types

Variables are things that vary.  The following are some of the common variable types you will use when programming in objective-c.


Short for ‘integer’ an int is a whole number with no decimal point, for example, ’89’.  The objective-c code to declare a new int variable called myIntVariable with a value of 89 is:

int myIntVariable = 89;

Note that the variable name myIntVariable didn’t start with a capital letter, however the rest of the words in the variable did? That’s standard practice for variable names.  Oh and you need to also end every line of code with the ; symbol.


Short for ‘floating point’ a float is a number with a decimal point, for example, ’23.51369′.  When you display a float you can chop off some numbers you don’t want, for example, ’23.5′.  The objective-c code to declare a new float variable called myFloatVariable with a value of 23.51369 is:

float myFloatVariable = 23.51369;

Not much difference between declaring an int and a float.

Read more about standard variable (data) types here


An NSString stores a string of characters, for example ‘The sky is blue’.  NSString is actually a class (unlike an int or float).  When you use any class, you create an instance of the class.  The objective-c code to declare a new NSString class instance called ‘myString’ with a value of ‘The sky is blue’ is:

NSString *myString = @"The sky is blue";

As you can see there are a few new things here.  Firstly, there is a * in front of the variable name.  This means that the variable’ myString’ is actually a pointer to an instance of the NSString class.

If we were talking about animals then ‘cat’, ‘dog’ and ‘mouse’ would be instances of the class ‘Animal’.  I might store pointers to these class instances and call them animalOne, animalTwo and animalThree.

If you don’t understand yet don’t worry, just remember that NSString needs a * in front of the class instance name.

The other thing strange about the NSString class instance declaration is the @”…” around the string value.  Putting @”…” around a string is the standard way to indicate where a string starts and stops.  You will see this a lot.

Finally, you’re probably wondering ‘why is it called NSString? why not just String?’.  Well, that’s because when someone creates a class they usually prefix all their classes with an initial or two.  In this case the NS is short for NeXTSTEP.  You will see this practice a lot.  For example, all the cocos2d classes are prefixed with CC.  I prefix any classes I might re-use with TR.  The whole point of this is so that classes you make have less chance of having a conflicting name with a class someone else makes.  That would be bad news if you wanted both in one project.


As you can probably guess NSNumber was made by the same people who made NSString.  An NSNumber is a class that stores a number.  That number can be based from a float, int or a bunch of other things.  Why use it? well, it’s a little more flexible than just an int or float.  With an NSNumber you can convert to another type of number or even a string quite easily.   The objective-c code to declare a new NSNumber from an int value of 45 is:

NSNumber *myNumberValue = [NSNumber numberWithInt:45];

Hold your horses, what just happened?  We’re quite comfortable with the stuff on the left of the equal sign, but what’s this square bracket business on the right?  I’ll tell you.  This is objective-c messaging.  Remember that NSNumber is a class? well, numberWithInt is a class method that takes an int variable of 45.  Under the hood this class method does some stuff and spits out a lovely shiny new NSNumber that we’re now pointing to via the myNumberValue variable.  If it hasn’t quite clicked yet, don’t worry.

Logging stuff to the console

Logging stuff to the console (what I call the log window) is easy.  I’ll need you to do the following in preparation for coding:

  1. Open up the xcode project provided in Part 1, here it is again (it’s the default cocos2d_box2d template with a project name ‘Testing’).
  2. Expand the folder called Testing (or whatever your project is called)
  3. Click

The right hand window should now show the contents of  There will be a section like this:

- (void) applicationDidFinishLaunching:(UIApplication*)application {
//... lots of crap you don't understand here...
// Run the intro Scene
[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];

What you’re looking at is the contents of a class method (a.k.a. function) that is run when your app starts up.  Don’t worry about the structure of the thing just yet, all I want you to do is put the cursor just before the closing } and after the [[CCDirector…]] stuff.  The { } symbols mark the start and end of each method in the class.

Actually, put in two lines of  ////////////////////// with a line or ten between them, maybe even with a comment, like this:

- (void) applicationDidFinishLaunching:(UIApplication*)application {
//... lots of crap you don't understand here...
// Run the intro Scene
[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];
/////////////// MY CODE STARTS /////////////////
/////////////// MY CODE STOPS /////////////////

That area we just created is where we will put our code in between.  You probably realised by now that double forward slashes are used to comment out a line.  Tip: use /*  and  */ to comment out huge areas.

Add the following line of code between in our new coding area:

NSLog(@"The sky is blue");

Click play (ie Run) and check out your console, it should have logged the words ‘The sky is blue’.  If it does give yourself a pat on the back.  If you were not using cocos2d you would log using the NSLog command.  We’re using cocos2d though, so we use CCLOG instead:

CCLOG(@"The sky is blue");

Either way the formatting and all that is the same.  Make sure you get the capitalisation right, C (and therefore Objective-C) is case sensitive.

Lets do something trickier .  Here’s how you display an int variable to the console log:

int myIntVariable = 89;
CCLOG(@"%i", myIntVariable);

That will simply display the current value of myIntVariable, which is 89.  Note the %i means ‘integer goes here’.   You can pop some text in to make the console log line mean more to you, like this:

int myIntVariable = 89;
CCLOG(@"That variable of mine equals %i which is just great", myIntVariable);

The output to the console from that will be:   “That variable of mine equals 89 which is just great.” 

So what about multiple variables?  easy!

int myIntVariable = 89;
int myOtherIntVariable = 47;
CCLOG(@"The first variable is %i and the second is %i", myIntVariable, myOtherIntVariable);

The output to the console from that will be:  The first variable is 89 and the second is 47.

Different variable types require different letters immediately after the %.  To get a full listing of all the magic ‘format specifier’ possibilities, numb your mind with this post.  The only ones you’ll probably use early on are:

  • %i for integers
  • %f for floats
  • %@ for objects (like NSString)

I might wrap part 3 up now, it’s getting late and I think we’ve covered a lot.  Try using each of the variable types explained and displaying their values to the console as some homework.  Extra points for working out how to do some maths on them before displaying to the console ;-)

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 4 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 June 29, 2011 in iOS Tutorials


    5 Responses to iOS Newbie Part 3 – OMG Theory

    1. Ben

      August 11, 2011 at 7:01 am

      Thanks Tim, your explanations are great. All I want to understand at present is just what you are providing to get a basic game project up and running. Looking forward to more and thank you for sharing.


      • Tim Roadley

        August 11, 2011 at 8:05 am

        Thanks Ben, glad you like it. I’m new to this tutorial writing stuff so if I make any glaring mistakes along the way please don’t hesitate to tell me!

    2. Rick OSullivan

      June 5, 2012 at 5:52 pm

      Hi Tim,
      thanks mate. Another great TUT.
      1 question tho..
      With the use of %i variables how do you manage lots of integers ?
      I can see myself getting confused if I used more than a handful at a time.
      Thanks mate.

      • Tim Roadley

        June 6, 2012 at 8:46 am

        Hi Rixter,

        If you mean you need to store heaps of integers you’ll need to look into the Core Data tutorial set.

        If that’s not what you mean let me know



    Leave a Reply

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