Xcode Debugger Quick Look

A minor but interesting feature introduced to the debugger in Xcode 5.0 and extended in Xcode 5.1 is support for Quick Look to display variables. With a variable selected you can either use the small eye icon or use the space bar with the variable selected in the debugger variables view. A Quick Look popup window then shows you the contents graphically in a popup box. The example below is for UIColor class which shows you a small square filled with the color:

Other system classes that are supported include images, views, locations (displayed as a map), strings and attributed strings and URL classes (displayed as a web page).

Implementing Quick Look for Custom Types

Where this gets more interesting (and new in Xcode 5.1) is that you can easily support Quick Look in custom types by implementing -debugQuickLookObject and returning one of the supported types. The Apple documentation provides details on the supported types. As an example consider the custom table view cell I used in the Huckleberry project which consists of two formatted UILabel objects:

By default, the Quick Look for a table view cell does not show anything and if you print the description it is also often unhelpful:

Printing description of textCell:
<UYLTextCell: 0x1095a9bb0; baseClass = UITableViewCell; frame = 
(0 0; 320 100); layer = <CALayer: 0x1095a8b90>>

A quick first implementation could be to display the text of the two labels by returning an NSString:

@implementation UYLTextCell
- (id)debugQuickLookObject {
  NSString *result = [NSString stringWithFormat:@"%@: %@",
    self.numberLabel.text, self.lineLabel.text];
  return result;
}
@end

What is even better is if we show the attributed strings:

- (id)debugQuickLookObject {
  NSAttributedString *cr = [[NSAttributedString alloc] 
    initWithString:@"\n"];
  NSMutableAttributedString *result = [[NSMutableAttributedString alloc]
   initWithAttributedString:self.numberLabel.attributedText];
  [result appendAttributedString:cr];
  [result appendAttributedString:self.lineLabel.attributedText];
  return result;
}

This gives a nice quick way to check the formatting of an attributed string:

Updated 13 March 2014: To clarify that Quick Look support was first added in Xcode 5.0 and then extended to include more system types and custom types in Xcode 5.1