TweetFollow Us on Twitter

Getting Started: More on Xcode

Volume Number: 19 (2003)
Issue Number: 10
Column Tag: Programming

Getting Started

More on Xcode

by Dave Mark

Let's take the debugger for a spin. As we've done for the past few months, we'll work with the Sketch sample project. As a reminder, the Sketch files live in /Developer/Examples/AppKit/Sketch/. Launch Xcode and open the project Sketch.pbxproj.

Setting a BreakpointWorking with the Debugger

When the project window appears, click on the Project Symbols smart group (left side of the window in the Groups & Files list). When your symbols list appears, click in the search field (right side of toolbar) and search for the string init. As you learned last month, this will winnow the list of project symbols down to those containing the string init.

Figure 1 shows this list with the init in the file SKTGraphic.m selected. This is the file we're going to edit and debug. Double-click this line so an editing window appears, listing the contents of SKTGraphic.m.


Figure 1. Search the Project Symbols for files containing init.

Setting a Breakpoint

Scroll about one quarter of the way down the source file and click on line 178. As a reminder, the navigation bar is the strip immediately above the main editing pane. The line number follows the file name, which is immediately to the right of the navigation arrows (see Figure 2). Once you locate line 178, set a breakpoint by clicking in the left column, to the left of the line in the editing window. Take a look at Figure 2. Note that the breakpoint arrow appears to the left of line 178. This means that the debugger, once started, will stop execution just before it executes the if statement on line 178.


Figure 2. Line 178 is selected. Note the line number in the navigation bar and the breakpoint set on the left.

To start the debugger, click the Debug icon in the SKTGraphic.m editing window's toolbar. Depending on things like the speed of your machine, how much of the program is already compiled, etc., this may take a bit. Be patient.

Once your code is built, the Sketch app is launched. Click on the rectangle tool and drag out a rectangle. As you release the mouse button, the debugger will hit that breakpoint and bring the Debug window to the front. Figure 3 shows the Debug window when you hit the breakpoint.


Figure 3. TheDebug window, showing the program paused at a breakpoint. Note the display of Locals in the upper-right pane.

Take a look at the upper-right pane in Figure 3. Notice the display of variables. You can manipulate the column widths by dragging on the splits between the Variable, Value, and Summary column headers. I maximized the width of the Value column in Figure 3. You can click on the disclosure triangles to reveal fields within structs. For example, origin has an x and a y component. The origin line lists the values of all the origin fields. Open the triangle and each field gets its own line.

If you double-click on the x value, the value of x turns into a text editing field allowing you to modify the value of x. Just as you'd expect.

But if you double-click on the origin value (the one that lists x and y values separated by a comma), the origin formatter will appear in a text editing field. In this case, the formatting string is:

x=%x%, y=%y%%origin%, %size%

Double-click the size value column and the size formatter will appear:

width=%width%, height=%height%

Let's change the formatter. Change it to read:

w=%width%, h=%height%

When you finish the edit (hit enter), the change you made will propagate up through both the display of size and any other displays that reference size. To see this for yourself, click the Arguments triangle, then the self triangle, then the _bounds triangle to reveal origin and size within _bounds. On my display (see Figure 4), the _bounds value field is:

x=50, y=63, w=107, h=169

When we changed one size formatter, the change propagated to all fields that displayed a value of type size. Data formatters are tied to type definitions, which are global.


Figure 4. In this shot, notice that self._bounds.size uses the modified size formatter.

    If you are unfamiliar with formatters, grab your favorite C text and do a bit of digging. Just like a formatter in a printf(), Xcode formatters allow you to specify how the debugger displays your variables. There's even an API so you can write your own formatters. See the Xcode release notes for more info.

Debug Controls

Figure 5 shows the debugger controls you'll find at the top of your Debug window. Chances are, if you've ever used a debugger before, you'll recognize most of them. Terminate terminates the process being debugged, Restart terminates the process and restarts it from the beginning. Pause pauses and Continue continues execution from the current stopping point. Step Over executes the next line of code without stepping into any functions, while Step Into steps to the next line of code, even if it means stepping into a function. Step Out completes the current function and stops immediately after the function's return in the calling function.


Figure 5. The debugger controls at the top of the Debug window.

The (in my opinion) coolest icon of the bunch, the Fix scotch tape dispenser is the equivalent of selecting Fix from the Debug menu. As I mentioned in last month's column, there appears to be a glitch with this icon in the Jaguar build of Xcode which I believe is fixed in the Panther build. Once Panther is officially released, I'll definitely be working exclusively with the Panther tools, so we'll get a sense of what's what in the current release of the tools.

The Breakpoints icon brings up the Breakpoints window (see Figure 6), which lists all your current breakpoints, organized by source file. You can turn breakpoints on and off using the checkbox associated with each breakpoint.


Figure 6. The Breakpoints window.

The Console Drawer icon opens a console drawer below the Debug window, allowing you to directly enter gdb commands. This is extremely cool, especially if you come from the Unix world and learned debugging using gdb, adb, or the equivalent.

    You've got the gdb doc on your hard drive:

    /Developer/Documentation/DeveloperTools/gdb/gdb/gdb_toc.html

Once More Into the Breach

Let's take one more spin through the debugger. If Sketch is currently debugging, click the Terminate icon. Now restart Sketch by clicking the Debug icon. When Sketch starts running, do not do anything in Sketch, just use the dock to return to Xcode. Back in Xcode, select Debug - ... from the Window menu to bring the Debug window to the front. Now, click on the Pause button to stop Sketch at whatever line was executing when you clicked the Pause button.

Figure 7 shows the event trace pane which is a stack showing the call sequence with main at the bottom and the current function at the top (in this case, the trap mach_msg_trap). Basically, this is what your app looks like when it is in an idle state, waiting for an event to happen.


Figure 7. The event trace pane shows call sequence stack.

Next, click the Continue icon to get Sketch back up and running. Back in Sketch, click on the rectangle tool and drag out a rectangle. When you let go, you should pop back into the debugger and your familiar breakpoint.

Click Step Into a few times until this line is highlighted:

temp = _bounds;

Remember, since this line is highlighted it has not yet executed. Click Step Into one more time. As you can see in Figure 8, when you execute this line and assign a value to temp, the temp fields displayed in the Value column are displayed in red, showing that they have changed.

If you click Step Into one more time, you'll change bounds. To see this change, you'll need to open the self triangle. self._bounds will turn red and temp will return to black.


Figure 8. When we change the value in temp, the temp fields turn red.

For our final trick, click on the Console Drawer icon. Scroll to the bottom of the window till you see this prompt:

(gdb)

At the prompt, type the command continue and hit return. This is exactly as if you had clicked the Continue icon in the toolbar. You are now back in Sketch and you can drag out some more shapes, as you like.

Till Next Month...

Whelp, I'm out of space again. <sigh>. There's so much more I want to talk about. Next month, we're going to take the debugger through its paces and we'll also play around with a feature called code completion, one of my very favorite parts of XcodeYou know, there are way more things I want to write about than there is space in the magazine. Note to self - see about adding more pages to mag. Ah, well. I've got a cool column idea for next month. Not sure I can pull it off. We'll See see. Look for you then...


Dave Mark is a long-time Mac developer and MacTech contributor. Author of more than a dozen books on various Mac-development topics, Dave is all about Xcode these days. Last month's column focused on the editor interface as well as fix-and-continue. This month's installment will take the debugger through a few of its paces, and explore Xcode's code completion feature.

 
AAPL
$98.77
Apple Inc.
-0.26
MSFT
$44.00
Microsoft Corpora
+0.03
GOOG
$589.11
Google Inc.
-1.49

MacTech Search:
Community Search:

Software Updates via MacUpdate

OS X Yosemite Wallpaper 1.0 - Desktop im...
OS X Yosemite Wallpaper is the gorgeous new background image for Apple's upcoming OS X 10.10 Yosemite. This wallpaper is available for all screen resolutions with a source file that measures 5,418... Read more
Acorn 4.4 - Bitmap image editor. (Demo)
Acorn is a new image editor built with one goal in mind - simplicity. Fast, easy, and fluid, Acorn provides the options you'll need without any overhead. Acorn feels right, and won't drain your bank... Read more
Bartender 1.2.20 - Organize your menu ba...
Bartender lets you organize your menu bar apps. Features: Lets you tidy your menu bar apps how you want. See your menu bar apps when you want. Hide the apps you need to run, but do not need to... Read more
TotalFinder 1.6.2 - Adds tabs, hotkeys,...
TotalFinder is a universally acclaimed navigational companion for your Mac. Enhance your Mac's Finder with features so smart and convenient, you won't believe you ever lived without them. Tab-based... Read more
Vienna 3.0.0 RC 2 :be5265e: - RSS and At...
Vienna is a freeware and Open-Source RSS/Atom newsreader with article storage and management via a SQLite database, written in Objective-C and Cocoa, for the OS X operating system. It provides... Read more
VLC Media Player 2.1.5 - Popular multime...
VLC Media Player is a highly portable multimedia player for various audio and video formats (MPEG-1, MPEG-2, MPEG-4, DivX, MP3, OGG, ...) as well as DVDs, VCDs, and various streaming protocols. It... Read more
Default Folder X 4.6.7 - Enhances Open a...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click... Read more
TinkerTool 5.3 - Expanded preference set...
TinkerTool is an application that gives you access to additional preference settings Apple has built into Mac OS X. This allows to activate hidden features in the operating system and in some of the... Read more
Audio Hijack Pro 2.11.0 - Record and enh...
Audio Hijack Pro drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio with Audio Hijack... Read more
Intermission 1.1.1 - Pause and rewind li...
Intermission allows you to pause and rewind live audio from any application on your Mac. Intermission will buffer up to 3 hours of audio, allowing users to skip through any assortment of audio... Read more

Latest Forum Discussions

See All

To The End Review
To The End Review By Lee Hamlet on July 29th, 2014 Our Rating: :: A VICIOUS CYCLEUniversal App - Designed for iPhone and iPad To The End will test players’ patience, timing, and dedication as they try to navigate all 13 levels in... | Read more »
Traps n’ Gemstones Review
Traps n’ Gemstones Review By Campbell Bird on July 28th, 2014 Our Rating: :: CASTLEVANIA JONESUniversal App - Designed for iPhone and iPad Fight mummies, dig tunnels, and ride a runaway minecart to discover ancient secrets in this... | Read more »
The Phantom PI Mission Apparition Review
The Phantom PI Mission Apparition Review By Jordan Minor on July 28th, 2014 Our Rating: :: GHOSTS BUSTEDUniversal App - Designed for iPhone and iPad The Phantom PI is an exceedingly clever and well-crafted adventure game.   | Read more »
More Stubies Are Coming Your Way in a Ne...
More Stubies Are Coming Your Way in a New Update Posted by Jessica Fisher on July 28th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
The Great Prank War Review
The Great Prank War Review By Nadia Oxford on July 28th, 2014 Our Rating: :: PRANKING IS SERIOUS BUSINESSUniversal App - Designed for iPhone and iPad Though short, The Great Prank War offers an interesting and fun mix of action and... | Read more »
Marvel Contest of Champions Announced at...
Marvel Contest of Champions Announced at Comic-Con Posted by Jennifer Allen on July 28th, 2014 [ permalink ] Announced over the weekend at San Diego Comic-Con was the fairly exciting looking Marvel Contest of Champions. | Read more »
Teenage Mutant Ninja Turtles Review
Teenage Mutant Ninja Turtles Review By Jennifer Allen on July 28th, 2014 Our Rating: :: DULL SWIPINGUniversal App - Designed for iPhone and iPad The pizza power is weak when it comes to this Teenage Mutant Ninja Turtles game.   | Read more »
Exploration Focused Puzzle Game Beatbudd...
Exploration Focused Puzzle Game Beatbuddy Set to Make Transition from PC to iOS this September Posted by Jennifer Allen on July 28th, 2014 [ permalink ] | Read more »
PlanetHD
PlanetHD By Nadia Oxford on July 28th, 2014 Our Rating: :: SPACE MADNESSUniversal App - Designed for iPhone and iPad PlanetHD will keep players busy for a while, though its unpredictable physics are a handful to deal with.   | Read more »
This Week at 148Apps: July 21-25, 2014
Another Week of Expert App Reviews   At 148Apps, we help you sort through the great ocean of apps to find the ones we think you’ll like and the ones you’ll need. Our top picks become Editor’s Choice, our stamp of approval for apps with that little... | Read more »

Price Scanner via MacPrices.net

Apple updates MacBook Pros with slightly fast...
Apple updated 13″ and 15″ Retina MacBook Pros today with slightly faster Haswell processors. 13″ models now ship with 8GB of RAM standard, while 15″ MacBook Pros ship with 16GB across the board. Most... Read more
Apple drops price on 13″ 2.5GHz MacBook Pro b...
The Apple Store has dropped their price for the 13″ 2.5GHz MacBook Pro by $100 to $1099 including free shipping. Read more
Apple drops prices on refurbished 2013 MacBoo...
The Apple Store has dropped prices on Apple Certified Refurbished 13″ and 15″ 2013 MacBook Pros, with model now available starting at $929. Apple’s one-year warranty is standard, and shipping is free... Read more
iOS 8 and OS X 10.10 To Support DuckDuckGo As...
Writing for Quartz, Dan Frommer reports that Apple’s forthcoming iOS 8 and OS X 10.10 operating systems version updates will allow users to select DuckDuckGo as their default search engine. He notes... Read more
U.K. Hospital Using iPods and iPads To Record...
British news journal GazetteLive’s. Ian McNeal notes that the old “an apple a day keeps the doctor away” proverb is being turned on its head at http://southtees.nhs.uk/hospitals/james-cook/ James... Read more
13-inch 2.5GHz MacBook Pro on sale for $1099,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $1099.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $100 off MSRP. Price is... Read more
Roundup of Apple refurbished MacBook Pros, th...
The Apple Store has Apple Certified Refurbished 13″ and 15″ MacBook Pros available for up to $400 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. Their prices... Read more
Record Mac Shipments In Q2/14 Confound Analys...
A Seeking Alpha Trefis commentary notes that Apple’s fiscal Q3 2014 results released July 22, beat market predictions on earnings, although revenues were slightly lower than anticipated. Apple’s Mac’... Read more
Intel To Launch Core M Silicon For Use In Not...
Digitimes’ Monica Chen and Joseph Tsai, report that Intel will launch 14nm-based Core M series processors specifically for use in fanless notebook/tablet 2-in-1 models in Q4 2014, with many models to... Read more
Apple’s 2014 Back to School promotion: $100 g...
 Apple’s 2014 Back to School promotion includes a free $100 App Store Gift Card with the purchase of any new Mac (Mac mini excluded), or a $50 Gift Card with the purchase of an iPad or iPhone,... Read more

Jobs Board

Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.