debugging & xcode shortcuts · 2009. 10. 16. · •examine what happened, when your app has...

54
Debugging & Xcode Shortcuts iPhone and iPod touch Development Fall 2009 — Lecture 16

Upload: others

Post on 27-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Debugging & Xcode ShortcutsiPhone and iPod touch Development

Fall 2009 — Lecture 16

Page 2: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Questions?

Page 3: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Announcements

• Oral proposal presentation guidance posted Tuesday evening

• Picking presenter ordering this evening

• Assignment #6 out later this week

Page 4: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Today’s Topics

• Building & Running Code

• Troubleshooting

• Troubleshooting and Debugging

• Clang Static Analyzer

• Editor Shortcuts

• Refactoring

Page 5: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Notes

• I’m showing the relevant portions of the view controller interfaces and implementations in these notes

• Remember to release relevant memory in the -dealloc methods — they are not shown here

• You will also need to wire up outlets and actions in IB

•Where delegates are used, they too require wiring in IB

Page 6: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Nomenclature

Page 7: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Nomenclature

• Much of tonights material involves keyboard shortcuts

• Just to make sure that everyone is on the same, the following common symbols are used to represent the associated keys

⎋!the escape key

⌃! the control key

⇧!the shift key

⌘!the command key

⌥!the option key

↩!the return key

← ↑ → ↓ the arrow keys

Page 8: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Building & Running Code

Page 9: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Building & Running

• Most of you have probably already been using the keyboard shortcuts for building source code

• If not, they warrant repeating...

•⌘B performs a build of the project

•⌘↩ build and run the project

Page 10: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Dealing with Errors & Warnings

• If we have errors in our code, these are called out to us in Xcode with messages that look like this...

Page 11: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Dealing with Errors & Warnings

•⇧⌘B brings up build results window which contains all of the messages in a single window which we can use to jump to the code associated with each method

Page 12: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Dealing with Errors & Warnings

• Alternatively you can use the following keyboard shortcuts to jump between the lines of code referenced by these compiler errors and warnings...

•⌘= jumps forwards to the next line/message

•⌘+ (i.e. ⇧⌘=) jumps backwards to the next line/message

Page 13: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Running Code

Page 14: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Running Code• Again, I know that most of you have already been using the

keyboard shortcut for running your app...

•⇧⌘R opens the console and brings it to the foreground, then you can press ⌘↩ to build and run your app

• The console can get cluttered any you may wish to clear it occasionally, there are buttons on the toolbar for this, or you could use the keyboard sequence

• ⌃⌥⌘R clears the console

Page 15: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Running the Console Automatically

• There’s also a setting under preferences which you can bring up with ⌘,

Page 16: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Troubleshooting

Page 17: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

The “HERE” Log Message

• So, your app is crashing, but you’re not sure exactly where

• You might try to see if certain code gets executed by placing adding NSLog statement

• Though, things tend to get lost in the mess if you have too many log statements that end up looking like this...

• You’ll end up with something that’s not really scalable...

NSLog(@"HERE");NSLog(@"HERE 2");NSLog(@"foo");

2009-10-21 23:45:44.824 HangmanGame[39643:207] HERE2009-10-21 23:45:44.824 HangmanGame[39643:207] HERE 22009-10-21 23:45:44.825 HangmanGame[39643:207] foo

Page 18: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Slightly Improved “HERE” Log Message

• You can use some identifiers which the pre-processor replaces with data about your file

• For example, the following code...

• Outputs the slightly more useful...

• Can quickly copy and paste as needed without having to type a bunch of log statements and change the messages

- (IBAction)someMethodOrAnother:(id)sender { NSLog(@"%s:%d:%s", __FILE__, __LINE__, __FUNCTION__);}

2009-10-21 23:40:33.663 HangmanGame[39601:207] /Users/Dan/Desktop/HangmanGame/Classes/HangmanGameViewController.m:16:-[HangmanGameViewController someMethodOrAnother:]

Page 19: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Using the Debugger

• If you find the process of using log statements tedious (edit, save, build, run, repeat), then you may wish to use the built in debugger

• If that’s your cup of tea, you’ll be glad to know that Xcode has a great debugger integrated right into Xcode

Page 20: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

GDB

Page 21: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

GDB

• Same GNU Project debugger you’re accustomed to on Linux

• Allows you to see what is going on inside your program while it executes (or what it was doing when it crashed)

• GDB can do four main kinds of things...

• Start your app, specifying things that might affect its behavior

• Make your app stop on specified conditions

• Examine what happened, when your app has stopped

• Change things in your app, so you can experiment with changing the state of variables, etc.

Page 22: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Running GDB

• If you are used to pressing the Build button in the toolbar you can simply hold down the ⌥ key while pressing the button to toggle between normal and debug modes...

• If your app crashes the raw GDB text interface will be in the run console

• You’ll need to have the active configuration set to “Debug”

• This is the default unless you changed it

Page 23: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

If You Take One Thing Away...

• If your app crashes, there’s a good chance you can get a meaningful stack trace by issuing the where command...

Start from the top and work your

way down looking for your code

Page 24: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Basic Usage Demo

• Using the run/debugger console...

• Skip over, step into, step out of

• Continue

• Printing variables (p, po)

• Setting/removing breakpoints

• For an even more details you can bring the full blown debugger using the ⌘Y shortcut

Page 25: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

LLVM & the Clang Static Analyzer

Page 26: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console
Page 27: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

LLVM & the Clang Static Analyzer

•We’ve briefly talked about the LLVM project before

• One of the subproject called Clang has a great static analysis tool that can be used to automatically find bugs in your code

• The Clang static analyzer is now included in Xcode 3.2 that’s available for Snow Leopard

• In my opinion, this improvement alone is worth the upgrade price for Snow Leopard

• The static analyzer is also available as a stand-alone download for older versions of Xcode, though you’re not going to get the Xcode generation

Page 28: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Clang Static Analyzer

• To run the static analysis tool, you can a build and analyze using ⇧⌘A

Page 29: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Editor Shortcuts

Page 30: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Basic Editor Shortcuts

•We’ve already discussed numerous basic shortcuts, such as...

• Pressing ⎋ to bring up autocompletion

• Using ⌃/ to jump to the next section in an autocomplete

• Commenting/uncommenting sections of code by first selecting them and then pressing ⌘/

• Switching between header and implementation files by using ⌥⌘↑

Page 31: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Getting Help

•We’ve also discussed bringing up the help documentation window using the ⇧⌥⌘? shortcut

• Additionally, we’ve discussed in class using the following shortcuts which can be used from the editor window

•⌥ click on a method/class in Xcode to bring up the documentation for that method/class/etc.

•⌘ click on a method/class in Xcode to jump to that method/class declaration in its header

Page 32: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Editing Pane Options

• As you may have figured out, double clicking on a class in the Groups & Files pane will open that class in a new editor that fills the window

• There are also some other options for manipulating the size of the editor pane

•⇧⌘E will collapse the File Name pane causing the editor to take up the full height of the window

•⇧⌥⌘E will collapse both the Groups & Files and File Name panes causing the editor to be full window

• You can repeat the shortcut to toggle back and forth

Page 33: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Folding Blocks of Code

• You can collapse and expand blocks of code within Xcode with a couple of shortcuts (you can also do this from the gutter to the left of the source code)

• ⌃⌘← collapses the current block

• ⌃⌘→ expands the current block

• ⌃⌘↑ collapses all methods in a class

• ⌃⌘↓ expands all methods in a class

• There’s also a mode that shades out everything but the current block that can be toggled on and off via ⌃⌥⌘F

Page 34: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Position Related Items

•⌘L opens a dialog that lets you type in a line or column number and jump to that line of code in the current file

• ⌃L centers the current line in the middle of the buffer

Page 35: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Line and Column Numbering

• Line or column numbers can be enabled under preferences...

Page 36: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Navigating Between Files

• You can bring up the open files drop down with ⌃1• Just start typing file name, then press ↩

Page 37: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Navigating Between Files

• The forward and backward arrows atop the editor can be used to cycle between open files

• Can also use the ⇧⌃⌥← and ⇧⌃⌥→ shortcuts

Page 38: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Navigating Between Files

• There’s also a powerful open quickly window that lets you open any resource (including NIBs) directly from Xcode

• Use ⇧⌘D to bring up the open quickly window

• Just start typing the resource name and it will list matches

• Use ↑ & ↓ to change files, press ↩ to open

Page 39: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Navigating Within a File

• Similar to the file drop down, there’s also a function/method drop down that you can bring up by pressing ⌃2

Page 40: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Navigating Within a File

• You may have noticed that there were also labels mixed in with the function/method names

• These titles are inserting a #pragma mark statement...#pragma mark -#pragma mark Drawing Methods

- (void)drawCircle {!}

- (void)drawSquare {!}

- (void)drawTriangle {!}

@end

Ignored by the compiler, but

read by Xcode General form is:#pragma mark <title>

(a ‘-’ draws a separator)

Page 41: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Navigating Within a File

• The functions/method drop down with our title and separator section...

Page 42: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Navigating Within a File

• In addition to the #pragma mark style notations, we can also put special comments in the code and have then show up in the functions/methods menu as well...

// TODO: blah

// FIXME: blah

// ???: label

// !!!: label

Page 43: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Custom Key Bindings

• Xcode is very flexible and allows you to add your own new keybindings or even re-wire the existing bindings

• This can be configured under Preferences as well...

Page 44: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactoring

Page 45: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor Local Variables

• You can chance in scope instances of a variable in a given scope by first selecting the variable, then pressing ⌃⌘T

• This is draw a box around the variable and as you type, all instances of it in the scope will change...

Page 46: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor Instance Variables

• This change in scope feature works well for local variables, but doesn’t work for class level instance variables

• To refactor all instances of that ivar across a class you can use ⇧⌘J over the ivar to bring up the refactoring dialog...

Page 47: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor Instance Variables

• Renaming an ivar via refactoring has the added benefit that it will even go into the NIB file and make changes there

• Additionally, you can also preview (and approve or deny) what’s going to change before Xcode actually does it

Page 48: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor Classes

• You can also use refactoring to rename classes

• In this case, in addition to renaming everything in the implementation, header and NIB files Xcode will also rename the files themselves for you...

Page 49: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor to Extract Code into Method

•We can also use the refactor command to automatically separate out a chunk of inline code to its own method

• Simply select the section of code and press ⇧⌘J...

Page 50: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor to Extract Code into Method

• This will do some analysis of the code and suggest a method signature based on what’s going on in the code

• Here’s the stubbed out method signature for our selection...

Page 51: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor to Extract Code into Method

•We can give it a name, and preview the changes...

Page 52: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Refactor to Extract Code into Method

• The result of refactoring and extracting some code into a method...

Page 53: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

Additional Resources

• Xcode Workspace Guide

• http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/XcodeWorkspace/

• Xcode Debugging Guide

• http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/XcodeDebugging/

•What's New in Xcode

• http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/WhatsNewXcode/

Page 54: Debugging & Xcode Shortcuts · 2009. 10. 16. · •Examine what happened, when your app has stopped ... •If your app crashes the raw GDB text interface will be in the run console

For Next Class

• Prepare for your initial project presentation