Xcode 4 : Show Invisibles to Find Tabs and Spaces

Every now and again I experience some rather odd code formatting behavior when I copy/paste from Xcode into another editor, an email or this blog. I’m never quite sure just where the weirdness originates, most likely an errant tab that was inadvertently introduced somewhere on the way.

For example, this screenshot from Xcode looks rather innocuous:


However, if I copy and paste the same code into the editor that I am using to write this post, the formatting looks as follows:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{   
  // Create and initialize the window
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  window.backgroundColor = [UIColor blackColor];
 
	// Create test view controller
	vc = [[MainViewController alloc] init];
 
  [window makeKeyAndVisible];
  [window addSubview:[vc view]];
 
}

Within Xcode 4, in the Editor drop-down menu there is an option to Show Invisibles. Once enabled, you can see where tabs and spaces are embedded within the code.

Space characters are shown as a diamond shape – the red boxes (I added) highlight where tab characters are embedded in the source code, resulting in the funky formatting shown above. Replace the tabs with spaces and all is well.