I just tracked down a string equality bug due to string length mismatch. The extra character was '\r' which doesn't show up in the output window in Xcode 4 at all. If it had, I wouldn't have to spend nearly as much time as I did tracking the issue down.
Is it possible to show whitespace characters in the output window? If so, what magic incantation must I recite to enable it?
I tried Show Invisibles under the Edit开发者_C百科or menu, but that only worked for the code editors, not the output window. I'm using Xcode 4 on an iOS app.
I can think of only one solution right now: Creating a category:
NSString+myAdditions.h
@interface NSString (myAdditions)
- (NSString *)showInvisibles;
@end
NSString+myAdditions.m
#import "NSString+myAdditions.h"
@implementation NSString (myAdditions)
- (NSString *)showInvisibles
{
NSString *regexToReplaceWhitespaces = @"([\\s])";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexToReplaceWhitespaces
options:NSRegularExpressionCaseInsensitive
error:&error];
NSString *result = [regex stringByReplacingMatchesInString:self
options:0
range:NSMakeRange(0, [self length])
withTemplate:@"␣"];
return result;
}
@end
Usage
NSLog(@"show me the unseen: %@", [@"soo many whitespace in here \t\t <- look two tabs!" showInvisibles]);
Output
soo␣␣␣␣␣␣many␣␣␣␣whitespace␣in␣here␣␣␣␣␣<-␣look␣two␣tabs!
You could percent-escape your string. You can specify which characters to not escape in the third parameter (I included all of the common ones), the rest of which will be escaped:
NSLog(@"%@", CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)yourString, (CFStringRef)@" <>#%{}|\^~[]`;/?:@=&$", NULL, kCFStringEncodingUTF8);
精彩评论