You’re Doing It Wrong

In IRC, someone noted how they just learned about gdb’s print-object (or po) command. It was surprising since this was an experienced dev. It just goes to show that no matter how long you’ve been programming, there’s always some thing you should know, but don’t. These are those commands or features you just somehow missed. You bitch about what a pain in the ass something is only to realize that there was a simple solution all along.

So, in the interest of full disclosure, here are a couple of mine.

NSStringFromRect(), NSStringFromRange(), NSStringFromPoint(), et al.

Stupidly, whenever I needed to log an NSRect, NSRange, etc., I’d list out each of the components. For NSRect, it was always the worst, as it would look like this:

NSLog(@"%f %f %f %f", NSMinX(rect), NSMinY(rect), NSWidth(rect), NSHeight(rect));

I could have put this in a #define but for some twisted reason I would just keep copying it from other places or worse, typing it out. I finally learned about the above functions and now my teeth are whiter than ever!

Dragging header files into IB

For years, when I needed to have the header file for my class read into IB, I’d click on the “Classes” tab, select my class, do “Read Files…” (because of peculiarities in my project, the “Read Something.h” item is not enabled) and select my file in the open panel. The step of clicking on the “Classes” tab seemed particularly ridiculous to me if you had an instance of the class selected in the “Instances” pane.

At the last Cocoaheads meeting, Cathy Shive was giving a presentation on doing custom controls. While demonstrating how she put together the nib, I watched in amazement as she dragged the header file into the IB window to update her outlets and actions. I suppressed a sob as I reflected on all the time I wasted.

Command-double-click and option-double-click in XCode

In XCode’s editor, command-double-clicking on a symbol will bring up its definition, opening the appropriate header file as needed. Option-double-click brings up its documentation. You know. Me no know. Me ashamed.

• • •

Maybe these are news to you as well and if so, then I feel slightly less dumb. There are probably worse cases than this but I have erased all traces of them from my memory.

What are your embarrassing discoveries?

Category: Cocoa, Debugging, OS X, Programming 10 comments »

10 Responses to “You’re Doing It Wrong”

  1. Blake Seely

    For structures like NSSize and NSRect, you can also just type ‘p ‘, and gdb will print out all the members for you.

  2. Blake Seely

    ER, I guess I shouldn’t have used angle brackets in that post 🙂 What it should say is just type ‘p (name of your nsrect var here)’ and gdb will print the right thing

  3. Chris Ryland

    Since the Leopard embargo is lifted as of today (at least that’s what I read on some rumor site ;-), in IB3 I think it will auto-sense any changes you make to your header files when you switch back into it. No more d’n’d needed.

  4. mr_noodle

    Blake: Yes, ‘p’ does work nicely in gdb though I still don’t understand why I need to cast the return types on methods all the time. Hmm, maybe I’m about to make another embarrassing discovery…

    As for NSLog debugging, I have to use it when dealing with mouse loops and such where you can’t just switch to another window and type stuff.

    Chris: I keep hearing about this embargo but I wonder if it’s only applicable to journalists who signed a different NDA than the one we did? I don’t recall the ADC NDAs having a date in them. In any case, thanks for the info.

  5. Andy Lee

    Cathy did something else in that demo that I didn’t know you could do. I guess my embarrassing discovery is that I don’t remember what it was.

  6. Steve Peter

    Andy, was it beginning a drag, then switching apps with Cmd-Tab, then dropping? I didn’t know you could do that.

    Paul, the title of this post reminds me of this great poster of John McCarthy in the style of those motivational posters:

  7. Andy Lee

    That was it! Amazing that you remembered that.

  8. ctshryock

    i didn’t know about option-double click. i don’t feel stupid, just smarter now 🙂

  9. Jordy/Jediknil

    Not exactly embarassing, since it’s not something most people would try, but my biggest aha! moment in Xcode was when I realized you could use Custom Executables to run /existing applications/…including a second copy of Xcode itself. Makes debugging plugins a heck of a lot easier.

  10. mr_noodle

    Thanks for reminding me of that one. As a developer of a pref pane, I learned about specifying System Preferences as an executable a bit later than I should have. I would always launch Sys Prefs separately and then attach to it. I sometimes still attach but it’s nice to just click “Debug” and have it run properly.

Leave a Reply

Back to top