Creating Custom Object Descriptions For Debugging

As you may be aware, every object in Objective-C has a description method that returns a string. This is similar to the toString method in languages like Java and C#.

The purpose of the description method is not really just to convert objects to strings; NSNumber has a stringValue method for that purpose, NSArray has componentsJoinedByString: and NSDate can be “stringified” using an NSDateFormatter, etc. The description method’s main purpose is for debugging.

You typically use the description method in one of two ways. Either in code, using NSLog, as follows:

Continue reading

Debugging NSAttributedString Using a Block to Print Attributes

In a post earlier this week, Create NSAttributedString from HTML in iOS 7, I outlined the basics for reading an HTML file and generating an NSAttributedString. This post includes a few tips for debugging an NSAttributedString.

Assuming you have an html file with the name test.html, the code for creating the NSAttributedString is as follows:

// Create URL from HTML file in application bundle
NSURL *html = [[NSBundle mainBundle] URLForResource: @"test" withExtension:@"html"];
// Create attributed string from HTML
NSAttributedString *attrStr = [[NSAttributedString alloc]
  documentAttributes:nil error:nil];

Debugging NSAttributedStrings

Should the output of the NSAttributedString not be what you expected, as is most always the case, debug information can provide some insight.

Continue reading

Display Debug Information In A Popup Window (UIAlertView)

I previously shared the code I use to replace NSLog, as I really don’t care for the date/time stamp that NSLog outputs to the console.

Every now and again rather than looking at the console, it can be helpful to have a popup (UIAlertView) with the same information that one would include in NSLog. Let’s see how to accomplish this…

Assume you have the following code:

NSString *str = @"iOS Developer Tips";
int x = 101;
NSLog(@"str: %@ x: %d", str, x);

The output will look as follows:

2013-04-07 23:28:42.404 Sandbox[18767:c07] str: iOS Developer Tips x: 101
Continue reading