Line In

Nomadic Designer

Learning Objective C: Moving From PHP

I’m in the process of learning Objective-C in order to get more in to Mac and iOS development. I have a dream to one day create a Monkey Island-style graphic adventure (I recently found these excellent notes of a talk given by William Turner who worked as a Senior Artist on the Curse of Monkey Island that are worth a read if you’re interested in the design side of things as well).

Before I get to my Magnum Opus, I’m starting small with a few basic (but hopefully useful) apps to get my feet wet.

Resources

I’ve been following the Stanford University Developing Apps on iOS course led by Paul Hegarty which is an excellent and detailed introduction.

I think it’s absolutely incredible that one of the world’s top University’s is making all of their lectures, course notes and assignments available on iTunes for free and I feel like this is a giant step towards realising the true potential of the internet.

You can grab the entire course here (apparently, it has been downloaded over 1 million times already).

Prerequisites

If you haven’t done any Object-Orientated programming before, it’s probably going to be a little too advanced as it assumes some experience up front. You might want to investigate PHP: Objects, Patterns and Practice by Matt Zandstra first.

Objective-C is based around the MVC (Model, View, Controller) paradigm and I figured it would be easier to get my head around the concepts using a language that I already understood.

I don’t really think WordPress is the best place for this so I investigated a few pure MVC frameworks and eventually settled on Cake PHP (although there are other great frameworks out there such as the Zend Framework and CodeIgnitor).

If you’re coming from OOPHP, I would highly recommend spending a few days learning MVC concepts within PHP before switching. The basic structure of projects in XCode made a lot more sense once I understood the central ideas to this paradigm and it was one less thing to worry about moving over.

PHP Vs. Objective-C

I’m going to be documenting my progress here and I thought I’d start with a random list of my initial impressions on the differences between the two languages.

  1. Objective-C is a compiled language vs. PHP which is a scripting language
  2. Pretty much everything in Objective-C is an object
    1. Strings are an object (not a primitive) in Objective C. Characters are still primitives, but not strings
  3. Classes have to be declared in their own files
    1. There needs to be a .h file, which is included wherever you need to use it, and is a header file where the interface is declared. Public instance variables and methods are declared here.
    2. This is coupled with a .m file, which is the implementation file where you put the implementations of the methods declared in the header files.
  4. Objective-C is a strictly typed language (that means declaring the *type* of variable before the variable name, for example: `$var = 21;` (PHP) vs `(int) var = 20;` (Objective-C).
  5. Declaring functions is a whole new ballgame. Forget `public myFunction(arg1, arg2)` you’re now looking at `-(void) myFunction:(int)arg1 TakesThis:(int)arg2;`
    1. Things to notice about this: the minus sign at the start means it’s an instance method (a plus means it’s a class method, like static methods in PHP)
    2. You have to declare what primitive type or object the function returns. `(void)` indicates that this method returns nothing.
    3. You have to declare what types the arguments should be (either primitives or objects).
  6. Accessing methods is likewise very different. `$var = myObject->get_result();` becomes `NSObject *var = [myObject getResult];`
    1. You’ll notice the asterisk in the last example. That’s not part of the variable name—it represents a pointer to a portion of memory where the object is stored.
    2. Speaking of memory, it used to be that you had to manage memory yourself. Thanks to Automatic Reference Counting (ARC), the language takes care of that for you.
  7. Generally, strings and arrays are immutable (meaning they can’t be changed once they’ve been created). There are mutable strings and arrays, but these are used a lot less often. If you’re used to working dynamically with arrays, this is a big difference.
  8. There’s no foreach (but there is fast enumeration, which in a lot of ways does the same thing).

Next, I’ll be looking at getting and installing XCode, exploring the documentation and creating an initial Hello, World iOS app.