XCode Autolayout

Leave a comment

There’s been some changes in XCode since I last did iOS.

Do get a basic layout, just drag your elements out onto the page.

Basically, hold down the control key, left click and drag off your image to the space around it, and some pop-ups should appear that will let you auto center.


Select center horizontally or vertically.

You can also control drag onto yourself.


You can also select some stuff in the menu to help resolve autolayout issues.


Now here’s the tricky bit. See that wAnyhAny thing at the bottom? Make sure it looks like this. If you mess with this and you don’t know what you are doing (like me) you will have some elements not appear when you run the simulator – basically because this governs whether you are on iPad, iPhone, landscape ect. So if you get this wrong, your elements may not ever show up (depending on the simulator hardware you select).


If you ever want to see all the elements you have on your view, click that little box in the lower right hand corner. You can then delete or do whatever you want with them.


iOS Boot Camp Coming to Montreal Nov 7-8

Leave a comment

November 7-8 me and my colleagues are going to Montreal for the first time offering our iOS Bootcamp at the Apple Regional office in St-Laurent.

This course is perfect for developers looking to get into iPhone development or just have an idea for an app they’d like build to turn into reality.

If you want to get into iOS development, and you would like a distilled, tight, 2 day course to get you there this is the course for you.

Signup now

See you there.


How to read a json file from unit test bundle in ios


Sometimes it’s handy to parse a json file as part of a unit test. Here’s how you it.

First create a test file containing the json you would like to parse:


Second, create a helper class to find your test file, and return an id object containing the json:


#import <Foundation/Foundation.h>

@interface TestingHelper : NSObject
+ (id)dataFromJSONFileNamed:(NSString *)fileName;


#import "TestingHelper.h"
#import "JSONKit.h"

@implementation TestingHelper

+ (id)dataFromJSONFileNamed:(NSString *)fileName
    //    NSString *resource = [[NSBundle mainBundle] pathForResource:fileName ofType:@"json"];
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSString *resource = [bundle pathForResource:fileName ofType:@"json"];
    if (NSClassFromString(@"NSJSONSerialization")) {
        NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:resource];
        [inputStream open];
        return [NSJSONSerialization JSONObjectWithStream:inputStream options:0 error:nil];
    } else {
        NSData *jsonData = [NSData dataWithContentsOfFile:resource];
        return [jsonData objectFromJSONData];


The important lines are these ones:

    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSString *resource = [bundle pathForResource:fileName ofType:@"json"];

These search your unit test bundle for the test file. Don’t do this.

NSString *resource = [[NSBundle mainBundle] pathForResource:fileName ofType:@"json"];

Unless you know what you are doing and have explicitly included that file to be copied to the bundle.

Then just call your test and make your assertions!

- (void)testJobPopulationWithJson
    NSArray *jsonArray = [TestingHelper dataFromJSONFileNamed:@"job"];
    [[JobRepository sharedRepository] extractJobsFromJson:jsonArray];

    // make your assertions
    STAssertTrue(1 == [[[JobRepository sharedRepository] enteredJobs] count], @"1 entered job.");

To test that your are getting your data from your file, set a break point and print out the array:

Links that help:

And thank you to Nick Waynik (@n_dubbs) for this tip.


For a video walkthrough of how to do this see:


dequeueReusableCellWithIdentifier no longer required iOS6


This is all we need to do now when grabbing a cell for display in a UITableViewController in iOS6:

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @”carTableCell”;
CarTableViewCell *cell = [tableView

// Configure the cell here …

return cell;

We used to have to do this (but not anymore – iOS6 now takes care of this for us)

// This step no longer required in iOS 6
if (cell == nil) {
cell = [[CarTableViewCell alloc]

Note: You still need the above code if you are working with customer xibs and cells.

Links that help:

How to save NSMutableArrays in NSUserDefaults

Leave a comment

Two things to keep in mind with saving an NSMutableArrays with NSUserDefaults:

1. All objects returned from NSUserDefaults are immutable.

2. All objects with the array need to be serializable (or property values).

The first one means if you saving something mutable, you have to reconstruct it when you read it back because it is going to be immutable:

 NSArray *temp = (NSArray *)[self loadUserDefaultWithKey:PHOTOS_KEY];
    self.photos = [NSMutableArray arrayWithArray:temp];

The second one means that whatever you are persisting in your array, it needs to contain property values, or implement some archiving methods so the elements know how to archive, and unarchive themselves when they comes out of storage.

I got caught with this trying to say an ALAsset into a mutable dictionary, and then read it back later. Doesn’t work well because of the second issue.

Here are the error messages you will get.

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object’

Note that dictionaries and arrays in property lists must also contain only property values.

More links on the subject



It’s not about the unit tests


iOS developers generally don’t unit test. So why then do they as a community seem to enjoy a reputation of quality.

No unit tests. No continuous integration. No TDD.

That pretty much summarizes my last project. It was my first paying iOS gig, and not only did we not apply these cherished practices, we shipped a high quality product.

This really bugged me.

I had always thought unit tests were essential. But this experience with iOS seemed to challenge some deeply cherished assumptions I had developed over the years about writing software, and led me to ask myself some very uncomfortable questions.

Was I regressing in my development practices?
Were unit tests not essential?
Was there something different about iOS development?
And how did the iOS community ship quality product, without unit tests at their core?

These questions kept me up and night, and led to the soul searching contained within this article.

Challenged assumptions

Ten years ago I used to debate people on the merits of unit testing. Not any more. Unit testing has become such a common practice that you’d struggle to find a modern development platform that doesn’t have some sort of automated testing framework.

And why not? The benefits are obvious. You can release, change, and refactor your code with confidence. You save a ton on regression testing. And the feeling of security that comes from knowing you have a suite automated unit tests backing your every change is undeniable.

So imagine my surprise when I entered a community responsible for some of the worlds most loved mobile applications, only to discover they don’t unit test. Even more disturbing, they seem to be getting away with it!

What’s different about iOS?

While I won’t say developing apps on iOS is easier, there are a few things iOS developers have going for them.

1. Smaller screen size.

You can’t fit a lot on a mobile phone screen — there just isn’t a lot of room. The apps also tend to be smaller (many don’t even have a backend). This, when combined with a culture of minimalism, dramatically simplifies things in terms of code and data. There is simply less of both.

2. No legacy.

Mobile apps are so new, iOS developers typically aren’t burdened with 100,000 lines of legacy code the way enterprise developers sometimes are. This let’s them start from scratch unencumbered.

3. One language.

You can build an iOS application knowing only one language: Objective-C. The typical web developer needs no fewer than four or five languages to do anything other than static HTML.

4. A mature platform.

A lot of the heavy lifting is done for you in iOS. If you need to do something with photos, music, or Facebook it’s there. You just plugin.

5. Very visual.

This was probably the biggest difference for me coming from the enterprise. Instead of spending days wading through layers of architecture (mocking and unit testing every step of the way), iOS developer have almost no layers of architecture. They spend almost all their time at the UI layer.

That means the nature of the code they write is often visual. The only way to see if it works is to fire up the simulator and try it out.

As interesting as these differences are however, they still don’t tell the whole story. If they did, every mobile platform would enjoy this higher level of quality. Nope there’s something else going on. Something bigger.

Who cares?

When you do something for a long time, it’s easy to forget what people in your field once did without.

  • Architects used to work with slide rules.
  • Seafarers used to navigate by the stars.
  • Artists used to work solely with their bare hands.
  • Programmers used to program without unit tests (often at a much higher quality they you see today).

And yet in all these fields, practitioners were able to achieve what we today, with all our modern tools, would admit was a high level of quality in their work. What was their secret?

They cared.

These people simply cared more about their craft, and what they were doing, than their contemporaries. They ‘out cared’ the competition. And that is what I see in the iOS community.

They care more about the art.
They care a lot about the exact wording and spacing of text on buttons.
They care a lot of the speed and performance.
And they care a ton about affordances (like remembering where in the scroll list a user was when they put the application in the background).

Apple works very hard to make sure every developer in their ecosystem cares, and they give them the tools so they have no excuse not to.

They walk them painstakingly through how to creating beautiful art for their apps.
They share (and enforce) human interface guidelines for mobile application development.
They curate and block apps that don’t meet certain quality or standards.
And their former CEO was known for calling people up in the middle of the night and tweaking the color of a logo.

It’s in the communities DNA. Here is the letter you get on your first day starting at Apple.

These guys care. They care like artists care. And—I’m just citing my own experience here— the same can’t be said for other platforms I have been a part of.

What does this have to do with unit tests?

Absolutely nothing. And that’s my point.

What leads to quality is something much bigger—more than a collection of software engineering techniques or a collection of practices.

When I entered this community I was under the false impression that if you didn’t write software the way I did, you must be doing it wrong.

Instead I discovered a community that cared more about quality than I did, and that I still had a lot to learn about crafting a quality experience.

That’s what this whole things has taught me. It’s not about the practices. It’s about the spirit, intent, and in which they are applied. Used when applicable. Quickly abandoned when not.

Are unit tests an invaluable tool for writing great software? Heck yes.
Am I going to produce a poor product if I can’t unit test? Hell no.

And that is what this experience taught me. I need to be more than a collection of practices. I will unit test where I can (including iOS) and I will doing whatever else it takes where I can’t.

All I can say is to keep growing sometimes we need to challenge our most cherished assumptions. It doesn’t always feel good, but that’s how we grow, gain experience, and turn knowledge into wisdom.

The second you think you’ve got it all figured out you’ve stopped living.

If you want to see how far down the rabbit hole this whole discussion on quality can go, I suggest picking up a copy of Zen and the Art of Motorcycle Maintenance. It’s not an easy read. But it may change your life.

Oh ya, and keep unit testing.


Portuguese translation courtesy of Sylvestre Mergulhão.

Learning iOS via the Robots And Pencils Academy


Hi all,

I am very excited to announce the launch of a new service me and my friend Michael have been working on.

It’s called The Robots and Pencils Academy and it’s basically screencasts for people who wanting to learn iOS.

As a recent student of iOS, I know how challenging it can be to get started.

New language.
New tools.
New technology.

It can be overwhelming.

This is the website I wish existed when I got started, and because we love sharing what we’ve learned with others, these screencasts are the result.

Getting started

If you are looking for a gentle introduction to Xcode or iOS development in general checkout:

If you just want to see what Objective-C looks like watch:

and a some others we recently created can be found here.


We will also be offering two day bootcamps through the site. Next one is here in Calgary at the end of the month and another coming up in Winnipeg. You can read more about those courses here:


Thank you for listening. Please share this with anyone you think may be interested.


Jonathan & Michael

Older Entries Newer Entries


Get every new post delivered to your Inbox.

Join 334 other followers

%d bloggers like this: