Learning Objective C for iOS Devices – a Novice’s Experience.

I’ve been very quiet on the camera / posting front for the last few months, partly because the last trip that we had [to Italy at the start of June] was a wash-out, but mainly because of my latest hobby, which has been learning Objective C.

The experience has been more interesting than the actual result, if I’m honest, but I thought it would be worth cataloguing some of resources I’ve found most useful, and some of the bigger challenges that I came across along the way.

Just as a point of reference: I have worked as a programmer professionally, but it was a long time ago [in the late 90s]. I have no background in Object Oriented languages, which is something that the iOS acolyte has to get comfortable with from the get-go.

It’s also not my first attempt at learning the language. I tried around this time last year and hit a wall technically, and didn’t quite know what search terms I needed to compile in order to move forward.

Having read what I’ve written below, one glaring omission from my experience is the Apple documentation. While it’s top drawer, and there is a lot of it, it’s not aimed at the novice. [I think this is a common phenomenon in computing: a plethora of documentation that, if you understood fully, you probably wouldn’t be referring to in the first place.] As a consequence, I didn’t use it very much, and still commonly have to defer to Stack Overflow for something that I’ve found in the Xcode documentation that I don’t understand fully, or that I need an example of.

In terms of books, I have both editions of the Big Nerd Ranch’s iOS Programming. I know from some conversations at work that it’s considered the text for developers. Personally, I have struggled with it, for stylistic reasons that I can’t quite put my finger on. It may just be that, in order to establish certain foundations, it covers areas of the platform that I’m not very interested in. With both editions, I got about through half a dozen to 10 chapters or so, and peeled off to various internet based resources. The second edition’s release was timed somewhat unfortunately, with an update to Xcode that the authors obviously had no control over. What it meant in practical terms was that a much-used template, cited as a starting point for many of the applications, disappeared. The replacement template the authors made available then generated some odd warnings at the start of the development process. At least that’s my recollection. Despite the reservations, I think anyone who is considering learning Objective C for iOS should still buy the book.

I got a great deal of value from a series of tutorials on a site put together by Ray Wenderlich. There is a three part tutorial called ‘how to create a simple iPhone App on iOS’ which is an absolutely fantastic resource, and a great starting point for learning to code from a practical point of view. I took this tutorial as the starting point for my own app, and continue to get great use out of the site.

One of the challenges that the novice bumps up against very quickly is that, once you go off the beaten track – i.e., whatever tutorial or examples you are working from and trying to adapt – things tend to get tricky quite quickly. There is no getting away from the fact that iOS is a very rich, but also a potentially very complicated, platform. Given that the various frameworks are giving you access to touch and network events [for instance], you have to get comfortable with concepts like callbacks from the start. Also, for purposes of making the app look like it hasn’t crashed or frozen, you will probably have to do something with asynchronous message queues – a topic covered by the Ray Wenderlich tutorial very nicely.

So here are a few things that I bumped up against. The problem that caused me to grind to a halt last time round was dealing with the adaptation of the templates that Xcode provides. Let’s say you want to add a new screen, so you add the view using the storyboard [or xib] canvas, and then link it to a controller. How, then, do you pass data from one controller to another? This had me stumped for a solid couple of weeks, and while I found lots of tutorials, for one reason or another I couldn’t get any of them to work: I either couldn’t understand them, or they were predicated on approaches that I found I wasn’t able to adapt. The breakthrough I had was stumbling across a site by a developer called Tim Roadley. His tutorials are without doubt among the best that I’ve seen online and his explanation for implementing protocols and delegates between the view controllers [the answer to the question that I didn’t know how to ask] was expressed with absolute clarity in the introduction to his tutorial on core data.

Core data brings me on to the next big challenge, which is deciding how to save data locally. The platform provides a number of options for you here, and I took the simplest which is to use property lists. This does get a bit messy, for instance when you delete the last element from a data structure.

I’m quite sure that the way that I was loading data from the plist into data structures, and then saving it again [AKA deserialising and serialising] was fairly dumb. But, fundamentally, it worked. It also got me into territory which demonstrated how powerful the language is. For instance, I store elements that are displayed on screen in an array of custom objects, which, having loaded from the plist as an un-ordered dictionary, I needed to sort by one attribute of the object. Something that I thought was going to take about a week of hacking took about 4 lines of code – admittedly after quite a few hours of thrashing around on Stack Overflow for the answer.

The final point that is worth calling out is automatic reference counting, a memory management feature that is still relatively new. You will probably find that an enormous amount of libraries that you would like to integrate with don’t support it, which can lead to some pretty complicated consequences. For instance, as an example to get a bit of experience with networking, I thought it would be quite straightforward to point an app at the XML for a site’s RSS feed, parse out the title of each story, and then put the resulting text into a table. I gave up on this after about 6 hours. The ‘simple’ XML library didn’t support ARC. Online resources suggested that setting a ‘no arc’ compiler flag would work. It did – or at least it got me over the start line, but introduced other problems that forced me to abandon what I thought would be an interesting distraction.

So now that I have my little app pretty much finished, am I ready to submit to the App Store for fame and fortune? Nope: the idea at the centre of it is fundamentally daft, and was something that I only ever intended as a learning exercise. There is also a lot of refinement that it would need before it would be fit for public consumption. For example, to avoid the problem of deleting the last item from the plist, I pop up a box in the UI that says, ‘please don’t do this, it gives me personal problems’. Probably not quite the rigor Apple would be looking to apply to preserve their standards in user experience. These problems are totally soluble: it’s just a case of the app itself not really being worth the investment in time.

And it is time consuming. I’ve probably put about 40 to 60 hours into the app, time that could easily have been invested in the latest downloadable content for SkyRim :).

I’m not done with the platform: next time round it will be a lot quicker to make headway. I’m also quite interested to see what sort of functionality the camera exposes: building a little time lapse app would be eminently achievable, for instance. But I’m not planning on giving up the day job just yet!