Skip to content

Instantly share code, notes, and snippets.

@jayrhynas
Created October 30, 2015 03:02
Show Gist options
  • Save jayrhynas/0561805f331d6ead5070 to your computer and use it in GitHub Desktop.
Save jayrhynas/0561805f331d6ead5070 to your computer and use it in GitHub Desktop.
@interface NSDictionary (Unicode)
- (NSString *)unicodeDescription;
- (NSString *)unicodeDescription:(int)level;
@end
@interface NSArray (Unicode)
- (NSString *)unicodeDescription;
- (NSString *)unicodeDescription:(int)level;
@end
@implementation NSDictionary (Unicode)
- (NSString *)unicodeDescription {
return [self unicodeDescription:0];
}
- (NSString *)unicodeDescription:(int)level {
NSMutableString *prefix = [[NSMutableString alloc] init];
for (int i = 0; i < level; i++) {
[prefix appendString:@" "];
}
NSMutableString *str = [[NSMutableString alloc] init];
[str appendFormat:@"%@{\n", prefix];
for (id key in self) {
[str appendFormat:@"%@ %@ = ", prefix, key];
id val = self[key];
NSString *valStr;
if ([val isKindOfClass:[NSDictionary class]]) {
valStr = [(NSDictionary *)val unicodeDescription:level + 1];
} else if ([val isKindOfClass:[NSArray class]]) {
valStr = [(NSArray *)val unicodeDescription:level + 1];
} else {
valStr = [val description];
}
[str appendFormat:@"%@;\n", valStr];
}
[str appendFormat:@"%@}", prefix];
return str;
}
@end
@implementation NSArray (Unicode)
- (NSString *)unicodeDescription {
return [self unicodeDescription:0];
}
- (NSString *)unicodeDescription:(int)level {
NSMutableString *prefix = [[NSMutableString alloc] init];
for (int i = 0; i < level; i++) {
[prefix appendString:@" "];
}
NSMutableString *str = [[NSMutableString alloc] init];
[str appendFormat:@"%@(\n", prefix];
for (id val in self) {
[str appendFormat:@"%@ ", prefix];
NSString *valStr;
if ([val isKindOfClass:[NSDictionary class]]) {
valStr = [(NSDictionary *)val unicodeDescription:level + 1];
} else if ([val isKindOfClass:[NSArray class]]) {
valStr = [(NSArray *)val unicodeDescription:level + 1];
} else {
valStr = [val description];
}
[str appendFormat:@"%@,\n", valStr];
}
[str appendFormat:@"%@)", prefix];
return str;
}
@end
#import "Collections+Unicode.h"
NSLog(@"%@", [@{@"céréales": @"hi", @"test": @[@1, @"1"]} unicodeDescription]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment