Handling Errors in Xcode

Tutorials

Before we get too far down the road of writing interesting programs, let’s take a look at what happens when things go wrong. Imagine you wrote an app that was 100,000 lines of code (instead of just 1) and when you tried to run it, the app failed to work (it “crashed”). You knew there must be an error in your code somewhere. So you would begin looking at each of those 100,000 lines of code to try and find the error. What a time consuming process that would be!

Fortunately, it is very unlikely that you’d ever have to do that in Xcode. Xcode includes a lot of functionality to automatically identify potential problems in the code you write. We’re going to look at two ways Xcode helps you. That way if you make a mistake while doing the exercises in this book, you’ll understand the messages that Xcode is giving to help you.

Warnings

First, let’s create a situation that would generate an error.

1. In your HelloWorld app type this line after your NSLog() statement:

xcodeErrors1

We’ll get to how to use variables in the next section, so don’t worry what this line does for now. But go ahead and run your program again.

Even though the program runs, if you look at your code, you should notice a little yellow triangle next to the line of code that you just added. In Xcode, little yellow triangles are called warnings. Warnings are minor potential errors in your code. You can still Build and Run your app with warnings in it, but this is Xcode’s way of saying “I think there is something wrong here that you probably want to fix.”

What is the warning about?

2. Go ahead and click on the yellow triangle to see more information about the warning.
When you click on it you should see something that looks like this:

xcodeErrors2

The warning detail says, “Unused variable ‘myVariable’.”

What Xcode is trying to tell you is that it noticed that you declared a variable that you did not use later in your program. Your two choices to get rid of this warning are to get rid of that variable declaration, or actually use that variable in some way in your program.

3. Delete the “int myVariable;” line of code because we really don’t need it at this point.

Once you delete that line notice that your warning also disappears. Warnings are minor errors that you can ignore and run your app. (although you should deal with them before you are done with development). There is another type of error that cannot be ignored.

Critical Errors

Let’s create a different problem in our code.

4. Go to the NSLog() statement and delete the semi-colon at the end of the line.

Now try and Run your app. You should get an error that looks like this:

xcodeErrors3

A red circle signifies a critical error in your code. With a critical error, the compiler cannot go ahead and build your app until the error is fixed. Just like with warnings, Xcode is helpful enough to give you an indication where it thinks the error is. Also, just like with warnings, you can click on the red indicator to get more information about the error.

5. Click on the red indicator next to the NSLog() line.

Of course we already know what the error is. Every programming statement needs to end in a semi-colon and we deleted the one from this line. Xcode is telling you that it thinks you are missing a semi-colon here. Also, because this is a fairly common and easily-fixed error (everyone forgets a semi-colon now and then) Xcode has offered to automatically fix it for you if you want.

xcodeErrors4

You can click on the Xcode suggestion correction to have it make the fix for you, or you can insert the semi-colon yourself. Once you fix this error, the red warning will disappear and you can run your app.

Conclusion

One of the benefits of using Xcode is the great tools that are included in the IDE that help you find and fix errors in your code. As we move forward with the exercises in this book, you are bound to mistype something at some point. Hopefully, now you now how to make sense of the messages Xcode gives you so you can easily find and fix those errors.

Julian Bryant