Join Todd Perkins for an in-depth discussion in this video Using the debugger, part of Xcode 7 Essential Training.
- View Offline
- The debugger in Xcode usually opens automatically when you have an error in your code. So right here I've written three short lines of code, where I create an object, set it to nil, and then force unwrap it with an exclamation point and try to grab one of its properties. Let's see what happens when I test the code. Go in to hit the Run button, start it out. As the application builds, it builds fine, and then it starts to run, and we get this error. EXC_BAD_INSTRUCTION that means that we've tried to access an object that is nil.
When this line of code is reached while the application is running, the issue is detected, and the debug navigator is opened up on the left side of the screen. The Debug navigator enables you to look at the processes that caused the error, organized by threads. So here I have Thread 1, which is automatically expanded, and, the problematic code is actually highlighted here. But if I wanted to look at what called that code, I could step down through the stack.
So I'm at main right here, and at some point main called UIApplicationMain, and at some point that called another method, and another method, and another method, which eventually lead to calling viewDidLoad. If you're curious about what was caused in the whole chain of events that led to this method being called, you can see the jump bar at the top of the Debug area at the bottom of your screen. If you don't see the Debug area, you can click the button in the toolbar to show it.
Further, if the Debug area is not split into two, you can show both the left and the right sides of the Debug area by clicking the buttons at the bottom of the Debug area. So we want to make sure that we're showing the console and the Variables view. In the Variables view we can look at the elements inside of the affected code and their values. So here on this line we see that there's a local variable called test that's of the data type an NSObject that is optional and the value is nil.
If we look at the selected line of code which is line 18, we can see that we're trying to reference that variable, and as I hover over it you'll see that it's nil. So using this information we could fix the error and our code would run. For right now, I'm not going to make any changes. I just wanted to point out that when you have runtime errors in your code, when that code is executed you're going to see the Debug navigator open up and you're going to see the Debug area open up, and you can use this information to fix the bugs inside of your code.
This Xcode tutorial helps new iOS and OS X developers install Xcode and start writing and editing code. Author Todd Perkins also reviews the version control and storyboard features, as well as the basics of Interface Builder, Xcode's intuitive UI design tool. Plus, learn how to compile and debug apps, test apps in the iOS Simulator, and send your app to Apple to be published in one of its app stores.
- What is Xcode?
- File handling in Xcode
- Exploring the code editor
- Creating snippets
- Adding Git version control to your project
- Making interface connections to the code
- Using Interface Builder
- Creating an iPhone or iPad storyboard
- Compiling code with the LLVM compiler
- Sharing resources in a workspace
- Catching common errors with Analyze and Fix-It
- Using the iOS Simulator
- Preparing an app to be published