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
$517.96
Apple Inc.
-3.72
MSFT
$39.75
Microsoft Corpora
+0.57
GOOG
$536.44
Google Inc.
+3.92

MacTech Search:
Community Search:

Software Updates via MacUpdate

Starcraft II: Wings of Liberty 1.1.1.180...
Download the patch by launching the Starcraft II game and downloading it through the Battle.net connection within the app. Starcraft II: Wings of Liberty is a strategy game played in real-time. You... Read more
Sibelius 7.5.0 - Music notation solution...
Sibelius is the world's best-selling music notation software for Mac. It is as intuitive to use as a pen, yet so powerful that it does most things in less than the blink of an eye. The demo includes... Read more
Typinator 5.9 - Speedy and reliable text...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more
MYStuff Pro 2.0.16 - Create inventories...
MYStuff Pro is the most flexible way to create detail-rich inventories for your home or small business. Add items to MYStuff by dragging and dropping existing information, uploading new images, or... Read more
TurboTax 2013.r17.002 - Manage your 2013...
TurboTax guides you through your tax return step by step, does all the calculations, and checks your return for errors and overlooked deductions. It lets you file your return electronically to get... Read more
TrailRunner 3.8.769 - Route planning for...
Note: While the software is classified as freeware, it is actually donationware. Please consider making a donation to help support development. TrailRunner is the perfect companion for runners,... Read more
Flavours 1.1.10 - Create and apply theme...
Flavours is a Mac application that allow users to create, apply and share beautifully designed themes. Classy Give your Mac a gorgeous new look by applying delicious themes! Easy Unleash your... Read more
Spotify 0.9.8.296. - Stream music, creat...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue... Read more
SlingPlayer Plugin 3.3.20.475 - Browser...
SlingPlayer is the screen interface software that works hand-in-hand with the hardware inside the Slingbox to make your TV viewing experience just like that at home. It features an array of... Read more
S.M.A.R.T. for USB and FireWire Drives 0...
S.M.A.R.T. for USB and FireWire Drives is a kernel driver for OS X external usb or firewire drives. It extends the standard driver behaviour by providing access to drive smart data. The interface to... Read more

Latest Forum Discussions

See All

148Apps Live on Twitch: Pivvot’s Looper...
On our latest Twitch stream, we’ll be playing a pair of minimalist arcade games, one that just got a big content update in Pivvot, and another that was inspired by it in 15 Coins. Whitaker Trebella, creator of Pivvot, will discuss the new modes... | Read more »
Word Cubes Review
Word Cubes Review By Jordan Minor on April 15th, 2014 Our Rating: :: SQUARESVILLEUniversal App - Designed for iPhone and iPad Word Cubes is fine, but it is barely any different from any other word game.   | Read more »
PAX East 2014 – Desert Fox: The Battle o...
PAX East 2014 – Desert Fox: The Battle of El Alamein is Coming to iOS Soon Posted by Rob Rich on April 15th, 2014 [ permalink ] Shenandoah Studio has become one of the go-to developers for war games on iOS, with | Read more »
Tank of Tanks Review
Tank of Tanks Review By Carter Dotson on April 15th, 2014 Our Rating: :: TANKS A LOT!iPad Only App - Designed for the iPad This multiplayer game played on a single iPad is simple, chaotic fun.   | Read more »
PAX East 2014 – Dungeon of the Endless J...
PAX East 2014 – Dungeon of the Endless Just Might Have a Shot at an iPad Release Posted by Rob Rich on April 15th, 2014 [ permalink ] I think it’s fair to say that | Read more »
SideSwype Review
SideSwype Review By Carter Dotson on April 15th, 2014 Our Rating: :: ON YOUR SIDEUniversal App - Designed for iPhone and iPad SideSwype is a puzzler that takes inspiration from Threes, but becomes its own incredibly fun game.   | Read more »
PAX East 2014 – Bigfoot Hunter Invites P...
PAX East 2014 – Bigfoot Hunter Invites Players on a Wild and Wooly Photo Safari Posted by Rob Rich on April 15th, 2014 [ permalink ] Yeti. Sasquatch. Wendigo. | Read more »
Dungeon Quest Review
Dungeon Quest Review By Cata Modorcea on April 15th, 2014 Our Rating: :: NO STORY, BUT GOOD FUNUniversal App - Designed for iPhone and iPad Dungeon Quest does a lot of things right, but ultimately forgets about one of the core... | Read more »
Tempo AI and Speek Join Forces to “Kill...
Tempo AI and Speek Join Forces to “Kill the Conference Call PIN” Posted by Rob Rich on April 15th, 2014 [ permalink ] Today Tempo AI, makers of Tempo Smart Calendar, and | Read more »
Fighting Fantasy: Starship Traveller Rev...
Fighting Fantasy: Starship Traveller Review By Jennifer Allen on April 15th, 2014 Our Rating: :: A SIGNIFICANT VOYAGEUniversal App - Designed for iPhone and iPad Continuing the release of Fighting Fantasy titles, Starship Traveller... | Read more »

Price Scanner via MacPrices.net

Save $50 on Mac mini Server
B&H Photo has the 2012 Mac mini Server on sale for $949 including free shipping plus NY sales tax only. Their price is $50 off MSRP. Read more
PhatWare’s “Ultimate Writing App For iOS” Ren...
PhatWare Corp. has announced it has renamed its new WritePro word processing app for iPhone and iPad: WritePad Pro. The decision to change the app’s name to leverages the strong brand awareness and... Read more
Full Resolution Photo Editor Tint Mint 1.0 Re...
California based independent developer, Jeffrey Sun, creator of the iOS app Modern Editor, has released Tint Mint, a new photography app for editing enthusiasts. The app costs a dollar, and it packs... Read more
16GB iPad mini (Apple refurbished) available...
The Apple Store has refurbished 1st generation 16GB iPad minis available for $249 including free shipping. Both black and white models are available. Read more
Save $120 on the 27-inch 3.2GHz Haswell iMac
B&H Photo has the 27″ 3.2GHz iMac on sale for $1679.99 including free shipping plus NY sales tax only. Their price is about $120 off MSRP. Read more
Using a Mac Doesn’t Eliminate The Heartbleed...
Low End Mac’s Dan Knight notes that any time you visit a website with an https: prefix or see that secure lock icon on your browser, some type of security software is busy trying to protect your data... Read more
AirPrint Basics Tutorial Posted
A new Apple Knowledge Base article helps get you started using AirPrint, the Apple protocol that enables instant printing from iPad, iPhone, iPod touch, and Mac without the need to install drivers or... Read more
Speed Tips For Running LibreOffice On The Mac
LibreOffice is my favorite of several free, open-source application suites, and the one I have configured on my Mac as my default app for Word documents that one frequently has to deal with. It also... Read more
Snag a 15-inch Retina MacBook Pro for $115 of...
B&H Photo has 2013 15″ Retina MacBook Pros on sale for up to $115 off MSRP for a limited time. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.3GHz Retina MacBook Pro: $2489.99... Read more
MacBook Airs on sale for $50 to $100 off MSRP
Several resellers are offering $50-$100 discounts on 11″ and 13″ MacBook Airs today, including Amazon, Best Buy, B&H, and others. See the breakdown of deals on our MacBook Air Price Tracker,... Read more

Jobs Board

*Apple* Retail - Market Leader - Cincinnati...
…challenges of developing individuals, building teams, and affecting growth across Apple Stores. You demonstrate successful leadership ability - focusing on excellence Read more
*Apple* Retail - Manager - SoHo - Apple (Uni...
Job SummaryKeeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you're a master of them all. In the store's fast-paced, dynamic Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** Every day, business customers come to the Apple Store to discover what powerful, easy-to-use Apple products can do for them. As a Business Leader, Read more
Position Opening at *Apple* - Apple (United...
…challenges of developing individuals, building teams, and affecting growth across Apple Stores. You demonstrate successful leadership ability - focusing on excellence Read more
Position Opening at *Apple* - Apple (United...
…Summary** As a Specialist, you help create the energy and excitement around Apple products, providing the right solutions and getting products into customers' hands. You Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.