I’ve been a bit silent on iOS development for several months now. There’s a reason for that, and it’s not because I haven’t been coding.
Right after finishing my last iteration, I found and read an Apple tutorial, Your Second iOS App: Storyboards. This was a really cool document showing how to create a Master-Detail application. The initial screen shows the contents in a list. Clicking on an object in the list brings up a second screen allowing you to edit the details of an object. In short, it’s an elegant implementation of what I had already created.
I immediately set about rewriting my application using the tutorial and the Master-Detail template provided in Xcode. Creating an iPhone version was easy, but I quickly stumbled with the iPad version. The tutorial didn’t cover the iPad, and the template-generated code was significantly different. The changes were good ones- using a Split View, which was on my to-do list anyway.
Once I had figured everything out, I had an application that should have worked, but it didn’t. I checked various online forums looking for hints that this was a bug or known problem, but found nothing. It then occured to me that I might have found a bug in Apple’s Xcode environment. Of course, I would need to provide a reproducible case, so I set about following the tutorial examples exactly, with the only change being that I wrote it for the iPad, not the iPhone. And… it worked perfectly.
I went back and fiddled with the editor application, trying to make it work. I spent a lot of time fiddling. No luck.
OK, time to start over. I went through the tutorial again (yes, I basically have it memorized at this point). This time I again tried to rebuild my editor application. And… it worked perfectly.
So I went back to my first (unsuccessful) attempt, and tried to determine how it differed from my second (succesful) attempt. Line by line, I changed the non-working app to use the same code as the working app. Still not working.
It wasn’t even failing in a rational way. It was failing on this line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
In the working app, it returned a cell. In the non-working app, it returned nil, and I couldn’t figure out why.
I converted the non-working app to use ARC (automatic reference counting). Nope, no change.
I noticed that the classes had different names in the two apps. So I renamed the classes in the non-working app. I figured I would do a file-by-file comparison and find out how they were different.
And now the non-working app immediately failed on startup. It threw an exception that it couldn’t find the MasterViewController class. This was curious, since I had renamed the MasterViewController class to be AttractionMasterViewController. A text search confirmed that there were no instances of MasterViewController anywhere in the source code. I tried the Clean option on the compiler- same error.
I deleted the application from the simulator and rebuilt it.
And the whole @$#^&%* thing immediately worked.
Lessons Learned:
* It might not be a compiler bug, but when things get wonky, try deleting the application from the simulator and trying again.
* It’s impossible to debug these kinds of issues in multiple 30-minute sessions. It took hours of focused work to track this down.
You should start seeing new posts about programming shortly.
*that's* worth remembering. You may have just saved me a few hours of debugging time – so thanks. Heck, you may even have saved my iOS client some money!!