Tag Archives: Xcode

How to define Preprocessor Macros in Xcode

I’ve often wondered how to use those efficiently, and I’ve just found out how to do it. As always, they’re not difficult to implement – but not documented in a way that simple folk like me can understand it. Be that as it may…

Preprocessor Macros can be useful if you’d like to compile two different versions of the same Xcode Project, such as a Lite and a Pro version, or a separate iPhone and an iPad version. Rather than create separate Xcode Projects for each version, you have one project with two targets. Each target can build a different version from your code, making maintenance much simpler than having to change the same code in two projects.

How do they work?

Preprocessor Macros are directives executed by the compiler, so they’re not part of Objective C. We’ve used them many times before with the #import statement. But to stay with our Lite and Pro example: you can use a Preprocessor if/then statement to check which version is being compiled. For this, let’s define a Macro for the Pro version. Here’s how in Xcode 4.6:

Preprocessor Macros

Let’s define one

Click on your Pro Target, head over to Build Settings and search for Preprocessor Macros. You’ll see something similar to the above screenshot. You can set Macros for each build configuration. By default we have two: Debug and Release. Notice that the Debug configuration already has a Macro defined – it’s called DEBUG=1. Therefore out of the box you can already check in your code if it has been compiled with the Debug or Release configuration.

To define your own Macro, click the little plus sign next Debug (and Release) and add something specific. I’m using IS_PRO=1, but you can choose anything you like really. I don’t know if you can set values other than boolean. Make sure you set your Macro in BOTH configurations, otherwise you’ll find different results when you submit your app.

How to check them in Code

Now that your Macro is defined, you can check if it’s present in your code like so:

And that’s all there’s to it! This makes most sense with Targets which we’ll discuss next.

What is an Exception Breakpoint

Sometimes Xcode just throws an error message that isn’t very helpful. That’s usually when we have to try and figure out why something didn’t go the way we expected it. Other times however, Xcode tells us very clearly which element it isn’t happy with – we fix it and our app progresses. Why can’t it always be that way?

Well that’s because at those unhelpful times, Xcode is technically picking up an “exception”, but it tries to continue to run the programme. That’s when we end up with an error message which isn’t related to the actual problem.

An Exception Breakpoint can help. It’s something we can set and therefore force Xcode to stop when the problem occurs, not several lines later. Here’s how we do it:

  • with the Navigator pane open, select the 6th tab from the left (the Breakpoint Navigator)
  • at the bottom left, click the plus sign to add a breakpoint
  • from the context menu select Add Exception Breakpoint
  • leave all default values and hit Done

Next time Xcode will not try to be heroic and keep going. Instead, expect a more helpful error message.

Screen Shot 2013-03-16 at 23.03.17

Also check out: https://developer.apple.com/library/ios/#recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html

Hilarious Xcode Error Message

Xcode_iconI’ve been working on an iOS App recently which deals with several date methods. Usually when something goes wrong Xcode displays very dry messages such as “Array out of bounds” or something rather unhelpful (like the complete stack output with no clue as to what actually went wrong).

I was accidentally passing nil into an NSDateComponents method – and instead of crashing (which is what I would have expected), Xcode displayed this super funny message in the log console:

[__NSCFCalendar components:fromDate:toDate:options:]:

toDate cannot be nil

I mean really, what do you think that operation is supposed to mean with a nil toDate?

An exception has been avoided for now. A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.

It made me smile 🙂

Continue reading Hilarious Xcode Error Message

How to fix “this class is not key value coding-compliant” after accidentally adding an IBOutlet instead of an IBAction

It has happened to us all: you’re in a storyboard, you’re using the Assistant Editor, you want to control-drag from a button into your code and create an action.

But sadly you forget to select “Action” from the drop down menu and instead create an outlet.

No biggie you think, deleting the outlet code. You go back and create the action properly, run the app… and get an error message such as this one:

*** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key badAction.’

Yikes! Unless you’re using Version Control, there is no way to undo adding that bad outlet. So you ask yourself: Will I have to start from scratch, or is there a way to fix your app?

Yes this is fixable – all we need to do is examine our storyboard file in Source Code mode, then find the problem and eliminate it. Here’s how you do it:

  • With the left pane open (Navigation Inspector, showing all your files), select your storyboard and right click/control click to bring up the context menu. Select View as Source Code


This will allow you to see what the storyboard actually looks like in XML. Hit Option+F to bring up the Find in Context menu. Now search for whatever is causing you a headache, then delete the entire line. Make sure the opening and closing brackets are selected too.

In my case, all this needs to go:

You may see more than one occurrence of your action in this file – we only want to get rid of the “outlet”reference here. If you delete all occurrences it’s not a big problem, but you will have to re-connect your actions to the code later.

Once you’re done, run the app again and all should be fine.

To display the storyboard in the visual editor again, just control-click the file file again, and select “Open as – Interface Builder, iOS Storyboard”.

When Xcode 4.5 suddenly stops running your app on a device (could not launch app)

I’ve had this problem that Xcode 4.5 would run my app fine in the simulator, but didn’t do so anymore on the device. Something about the “derived data folder was not found”. I checked, it was definitely there.

Then I found this post on Stackoverflow to show some suggestions: http://stackoverflow.com/questions/11456312/xcode-suddenly-stopped-running-project-on-hardware-could-not-launch-xxx-app

This solution worked for me – Thanks Dhilip: http://dhilipsiva.blogspot.in/2012/07/xcode-could-not-launch-app-no-such-file.html

How to use Xcode with a remote Git server

Xcode_iconImagine you’ve created a project in Xcode with a local Git repository. Now you’d like to put this online so that others can collaborate with you. How do you do that?

This has been puzzling me for a while, and there doesn’t seem to be a clear documentation on this subject – so I’ve decided to take some notes as I figured it out. Here are step by step instructions on how to make it work.

All we need is a server running Git and SSH credentials to that server to get started.

Continue reading How to use Xcode with a remote Git server