iOS developers, attention: a nasty bug I've discovered in Xcode 4

Today, I've decided to completely abandon the “old” 3.x series of Xcode, the development environment for iOS (and Mac OS X).

Up until now, I didn't want to switch as moving to the new Xcode required an almost complete re-learn of the system – even the basic keyboard shortcuts have been changed, to my “delight”.

Nevertheless, after some hours of angrily trying to find how a familiar feature needs to be accessed in the new environment, I actually came to like Xcode. For example, the Interface Builder integration makes it possible to auto-generate outlets and event handlers in both the .h and the .m (@synthesize directive for outlets and empty-bodied implementations for event handlers) file. All this fully automatically – all you need to do is supply the handler/outlet's name.

However, in the latest Snow Leopard build (4.2 4C199) of Xcode, there's an, it seems, undocumented bug I've discovered after at least 40-50 minutes of trying to find out why my connections don't work any more. If you auto-generate event handlers with the default settings (that is, with “id” as the component type):

and, later, remove the callback (click “X” in the component's event list, as usual), you may not be able to reconnect it again. The solution to this is keeping in mind that you always should use the actual GUI widget type as the parameter type when letting Xcode auto-generate the method stubs. (Or, if you forget to do this, you can later modify at least the declaration so that the callback method is found again by the system.)

I've dedicated a full video (it's HERE) to demonstrating this. In the video, I first create a one-page, Universal, barebones project. Onto the iPhone XIB, I drop a button at 0:38. At 0:43, I right-click the button to bring up the event / reference list and, at 0:46, I let Xcode create the new event handler declaration (and, in the .m file, the empty-bodied implementation). Note that I leave “id” as the default type during the creation and don't change it to UIButton. After this, at 1:00, I also (auto-)generate an outlet (I won't make use of it, though). At 1:16, I insert a NSLog() function call in the auto-generated event handler, eventH, so that I can see it's indeed invoked when the button is pressed. Then, after changing the default iPad target to iPhone, I run the app in the simulator to check if everything is OK. (It is – the debug message is indeed shown on the console.)

Now, at 1:43, I remove the callback reference. After this, I try to connect it again to the File's Owner on the middle left of the pane at 1:52 and 2:05, removing the reference between the two tests and after the second one too. (As you can see, re-wiring the event still worked in this case, showing it won't always turn up.) After this, I re-deploy the app to the simulator to check there is not any event callback any more (indeed there isn't).

At 2:55, I try to directly connect the event to the handler declaration in the .h file. As you can see (see the results on the console during the test run at 3:07), I too succeeded.

What starts happening with 3:26, however? As you can see, I try very hard to make the system recognize the event handler method, after removing the callback association, I try to wire the button and the handler together by first during dragging the line over File's Owner (it no longer gets highlighted and no methods are shown, showing the system doesn't think there's any suitable handler in the view controller), and, starting at around 3:28, in the .h file? Nothing, while both of these two wiring targets worked just fine some seconds before.

Now, at 3:37, I just change the “id” parameter type of the declaration to UIButton* in the .h file. (After this, there's a sizeable pause until 4:14: I had a phone call. Sorry, I haven't had the time to cut it from the video.) At 4:14, I retry wiring the callback to the method listed in the callback method list of the file's owner icon: it works, which is also evident at the test run at 4:38.

Xcode 4 learning resources for Xcode 3 programmers

I've tried reading thru all the Xcode 4 tutorials targeted at Xcode 3 programmers. Let me give you some tips and recommendations.

Moving from Xcode 3 to Xcode 4 – an easy, great comparison. While it's an additional article for an OS X (and not an iOS) programming book, it still contains a lot of useful information on, say, the automatic code insertion into the source code upon creating outlets or action targets. The entire article can easily be understood by an iOS programmer that has never ever programmed OS X.'s Xcode 4: the super mega awesome review - great! Note that the same folks have also published a quick list (in no way as exhaustive as the others) of the changes HERE

Xcode 4 Keyboard Shortcuts – a one-page, printable chart, great!

Apple's own “Xcode 4 Transition Guide” – as with Apple's official documentation in general, you might want to start with a more newcomer/beginner-friendly tutorial; for example, “Moving from Xcode 3 to Xcode 4” linked at the start.
Master your iPhone in one minute a day: Sign up here to get our FREE Tip of the Day delivered right to your inbox.

Author Details

Author Details

Werner Ruotsalainen

<p>Werner Ruotsalainen is an iOS and Java programming lecturer who is well-versed in programming, hacking, operating systems, and programming languages. Werner tries to generate unique articles on subjects not widely discussed. Some of his articles are highly technical and are intended for other programmers and coders.</p>
<p>Werner also is interested in photography and videography. He is a frequent contributor to not only mobile and computing publications, but also photo and video forums. He loves swimming, skiing, going to the gym, and using his iPads. English is one of several languages he speaks.</p>