Vincent Gable’s Blog

September 16, 2008

-description, Little-Known Hero of Debugging

Filed under: Cocoa,MacOSX,Objective-C,Programming,Sample Code,Tips | , , ,
― Vincent Gable on September 16, 2008

Or: How to Make NSLog() Useful With Your Objects

Say you have an archaically named NSArray that you want to inspect — it’s easy to do, since NSLog(@"The bestiary is %@", bestiary); prints out the array’s contents

2008-09-16 19:46:06.445 Tester[2678:10b] The bestiary is (
Cheetah,
Pumpa,
Jaguar,
Panther,
Tiger,
Leopard
)

But if you try to NSLog your own object, you get pretty useless output, like

myObject = <MyObject: 0x53f330>

Fortunately, it’s easy to fix! Just implement the method -(NSString*) description; and whatever it returns will be printed by NSLog and GDB (po object, will print object in GDB and the Xcode debugging console).

Here’s an (unfortunately complex) example,

- (NSString*) description;
{
  return [NSString stringWithFormat:@"<%@ %p> = {\n\tquestion=%@,\n\tanswer=%@,\n\tsource=%@\n}", [self className], self, self.question, self.answer, self.source];
}

output:

myObject = <MyObject 0x53eed0> = {
  question=What is the Best Thing Ever Of All Times, Ever?,
  answer=The Internet!,
  source=http://www.cabel.name/2008/01/2007-cabel-yay-awards.html
}

Useful Formatters and such

These macros have made my debugging-life easer.

%p tells NSLog to print the address of a pointer.

-className returns gives the name of a class as an NSString.

Don’t manually print out a Cocoa struct, ever, there are already NSStringTo* functions to do that for you, like NSStringFromPoint().

NSStringFromSelector() works as advertized (and paired with NSSelectorFromString() is very useful in general).

%lld tells NSLog to print a long long (64-bit integer). See also, printf reference.

%Lf tells NSLog to print a long double. See also, printf reference.

Best Practices

Whenever you make a new object, I strongly recommended immediately implementing a description method, and religiously keeping it up to date (it’s not hard, honest!). This won’t fix bugs, but it will make finding some of them much easier.

September 14, 2008

Fast Enumeration Really Is Faster

Filed under: Cocoa,MacOSX,Objective-C,Programming,Tips | , ,
― Vincent Gable on September 14, 2008

Your code will also run faster because the internal implementation reduces message send overhead and increases pipelining potential.

Matt Gallagher

I’ve written before about the advantages of a better enumeration paradigm, clearer code that’s faster to write, shorter, and with fewer bugs.

If you program in Cocoa, you should adopt fast enumeration post-haste.

August 13, 2008

Quickly Switching Between Headers and Source Files in Xcode

Filed under: MacOSX,Programming,Tips |
― Vincent Gable on August 13, 2008

Command + Option + Up-arrow will switch between a header and source-code file in Xcode. I wish I had figured this out years ago.

August 5, 2008

Simplified Logging

Filed under: Cocoa,MacOSX,Objective-C,Programming,Sample Code,Tips,Usability | , ,
― Vincent Gable on August 5, 2008

I have noticed a pattern in my Cocoa code, which I have been able to simplify. I often print out the value of a variable for debugging. 99 times out of 100, the code looks like this: NSLog(@"actionURL = %@", actionURL);, where actionURL is some variable.

But using the macros, I can say LOG_ID(actionURL);. This is shorter, and non-repetitive.

The macros I use to simplify debugging (2008-09-16):

#define FourCharCode2NSString(err) NSFileTypeForHFSTypeCode(err)

#define LOG_4CC(x) NSLog(@"%s = %@", # x, FourCharCode2NSString(x))
#define LOG_FUNCTION() NSLog(@"%s", __FUNCTION__)
#define LOG_ID(o) NSLog(@"%s = %@", # o, o)
#define LOG_INT(i) NSLog(@"%s = %d", # i, i)
#define LOG_INT64(ll) NSLog(@"%s = %lld", # ll, ll)
#define LOG_FLOAT(f) NSLog(@"%s = %f", # f, f)
#define LOG_LONG_FLOAT(f) NSLog(@"%s = %Lf", # f, f)
#define LOG_OBJECT(o) LOG_ID(o)
#define LOG_POINT(p) NSLog(@"%s = %@", # p, NSStringFromPoint(p))
#define LOG_RECT(r) NSLog(@"%s = %@", # r, NSStringFromRect(r))
#define LOG_SIZE(s) NSLog(@"%s = %@", # s, NSStringFromSize(s))

Look in assert.h for insight on how to roll your own debugging macros.

Neat iSight Trick

Filed under: MacOSX,Tips |
― Vincent Gable on August 5, 2008

Your iSight camera can see into the IR spectrum. You can see the invisible IR light on your remote control if you watch it thought your iSight.

July 15, 2008

Using a File Erasure Tool Considered Suspicious

Filed under: Quotes,Security,Tips | , ,
― Vincent Gable on July 15, 2008

I have often recommended that people use file erasure tools regularly, especially when crossing international borders with their computers. Now we have one more reason to use them regularly: plausible deniability if you’re accused of erasing data to keep it from the police.

Bruce Schneier

Finder -> Secure Empty Trash

July 14, 2008

Crash Reporter Prefs

Filed under: MacOSX,Reverse Engineering,Tips |
― Vincent Gable on July 14, 2008

/Developer/Applications/Utilities/CrashReporterPrefs.app can make crash reports much more useful. It will show crash-reports for background processes, and stack-trace in the notification dialog.

Crash report-files are stored in ~/Library/Logs/CrashReporter/

Unfortunately, crash reports are not sent back to third-party developers, even when you send them to Apple.

EDITED TO ADD: This still works in Snow Leopard (Mac OS X 10.5.6)

July 7, 2008

Rands First Law of Information Management

Filed under: Quotes,Tips,Usability | , , , ,
― Vincent Gable on July 7, 2008

Rands’ First Law of Information Management: “For each new piece of information you track, there is an equally old and useless piece of information you must throw away.”

Rands in repose

So true.

June 16, 2008

Backup

Filed under: Quotes,Security,Tips
― Vincent Gable on June 16, 2008

The single most important thing any company or individual can do to improve security is have a good backup strategy. It’s been true for decades, and it’s still true today.

Bruce Schneier

Here’s a good article on backing up data.

Hold off on Upgrading QuickTime to Version 7.5

Filed under: Announcement,MacOSX,Tips |
― Vincent Gable on June 16, 2008

The QuickTime 7.5 update appears to have broken sound in VMWare Fusion, and also in some code I’m working on. I will know more once I have fixed the issue in my own code. But for now, I advise waiting to update QuickTime to version 7.5.

UPDATE 2008-06-19: problems in my code solved. Basically I was specifying a bit-rate for a compressed audio stream, and QuickTime expected it to be 0. Earlier versions of QuickTime didn’t care, but 7.5 was more strict. Also, the VMWare problems have been fixed.

« Newer PostsOlder Posts »

Powered by WordPress