1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| @implementation NSDictionary (ZDLogHelper)
#ifdef DEBUG
- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
if ([NSJSONSerialization isValidJSONObject:self]) { NSString *logString; @try { logString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]; } @catch (NSException *exception) { logString = [NSString stringWithFormat:@"打印字典时转换失败:%@",exception.reason]; } @finally { return logString; } } NSMutableString *string = [NSMutableString stringWithString:@"{\n"]; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [string appendFormat:@"\t%@ = %@;\n", key, obj]; }]; [string appendString:@"}\n"]; return string; } #endif
@end
|