diff --git a/iOS/DoraemonKit/Src/Core/Category/Foundation+Doraemon.m b/iOS/DoraemonKit/Src/Core/Category/Foundation+Doraemon.m new file mode 100755 index 0000000000000000000000000000000000000000..69398a10014eead587db54861120becff9f3447a --- /dev/null +++ b/iOS/DoraemonKit/Src/Core/Category/Foundation+Doraemon.m @@ -0,0 +1,217 @@ +// +// Foundation+Doraemon.m +// DoraemonKit +// +// Created by DeveloperLY on 2019/8/18. +// + +#import + +#if DEBUG + +#define kUnicodeReadable 1 +#if kUnicodeReadable + +@implementation NSDictionary (UnicodeReadable) + +- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level { + NSMutableString *desc = [NSMutableString string]; + + NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level]; + for (NSUInteger i = 0; i < level; i++) { + [tabString appendString:@"\t"]; + } + + NSString *tab = @""; + if (level > 0) { + tab = tabString; + } + + [desc appendString:@"{\n"]; + + // 遍历数组,self就是当前的数组 + for (id key in self.allKeys) { + id obj = [self objectForKey:key]; + + if ([obj isKindOfClass:[NSString class]]) { + [desc appendFormat:@"%@\t%@ = \"%@\";\n", tab, key, obj]; + } else if ([obj isKindOfClass:[NSArray class]] + || [obj isKindOfClass:[NSDictionary class]] + || [obj isKindOfClass:[NSSet class]]) { + [desc appendFormat:@"%@\t%@ = %@;\n", tab, key, [obj descriptionWithLocale:locale indent:level + 1]]; + } else if ([obj isKindOfClass:[NSData class]]) { + // 如果是NSData类型,尝试去解析结果,以打印出可阅读的数据 + NSError *error = nil; + NSObject *result = [NSJSONSerialization JSONObjectWithData:obj + options:NSJSONReadingMutableContainers + error:&error]; + // 解析成功 + if (error == nil && result != nil) { + if ([result isKindOfClass:[NSDictionary class]] + || [result isKindOfClass:[NSArray class]] + || [result isKindOfClass:[NSSet class]]) { + NSString *str = [((NSDictionary *)result) descriptionWithLocale:locale indent:level + 1]; + [desc appendFormat:@"%@\t%@ = %@;\n", tab, key, str]; + } else if ([obj isKindOfClass:[NSString class]]) { + [desc appendFormat:@"%@\t%@ = \"%@\";\n", tab, key, result]; + } + } else { + @try { + NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding]; + if (str != nil) { + [desc appendFormat:@"%@\t%@ = \"%@\";\n", tab, key, str]; + } else { + [desc appendFormat:@"%@\t%@ = %@;\n", tab, key, obj]; + } + } + @catch (NSException *exception) { + [desc appendFormat:@"%@\t%@ = %@;\n", tab, key, obj]; + } + } + } else { + [desc appendFormat:@"%@\t%@ = %@;\n", tab, key, obj]; + } + } + + [desc appendFormat:@"%@}", tab]; + + return desc; +} + +@end + + +@implementation NSArray (UnicodeReadable) + +- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level { + NSMutableString *desc = [NSMutableString string]; + + NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level]; + for (NSUInteger i = 0; i < level; i++) { + [tabString appendString:@"\t"]; + } + + NSString *tab = @""; + if (level > 0) { + tab = tabString; + } + [desc appendString:@"(\n"]; + + for (id obj in self) { + if ([obj isKindOfClass:[NSDictionary class]] + || [obj isKindOfClass:[NSArray class]] + || [obj isKindOfClass:[NSSet class]]) { + NSString *str = [((NSDictionary *)obj) descriptionWithLocale:locale indent:level + 1]; + [desc appendFormat:@"%@\t%@,\n", tab, str]; + } else if ([obj isKindOfClass:[NSString class]]) { + [desc appendFormat:@"%@\t\"%@\",\n", tab, obj]; + } else if ([obj isKindOfClass:[NSData class]]) { + // 如果是NSData类型,尝试去解析结果,以打印出可阅读的数据 + NSError *error = nil; + NSObject *result = [NSJSONSerialization JSONObjectWithData:obj + options:NSJSONReadingMutableContainers + error:&error]; + // 解析成功 + if (error == nil && result != nil) { + if ([result isKindOfClass:[NSDictionary class]] + || [result isKindOfClass:[NSArray class]] + || [result isKindOfClass:[NSSet class]]) { + NSString *str = [((NSDictionary *)result) descriptionWithLocale:locale indent:level + 1]; + [desc appendFormat:@"%@\t%@,\n", tab, str]; + } else if ([obj isKindOfClass:[NSString class]]) { + [desc appendFormat:@"%@\t\"%@\",\n", tab, result]; + } + } else { + @try { + NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding]; + if (str != nil) { + [desc appendFormat:@"%@\t\"%@\",\n", tab, str]; + } else { + [desc appendFormat:@"%@\t%@,\n", tab, obj]; + } + } + @catch (NSException *exception) { + [desc appendFormat:@"%@\t%@,\n", tab, obj]; + } + } + } else { + [desc appendFormat:@"%@\t%@,\n", tab, obj]; + } + } + + [desc appendFormat:@"%@)", tab]; + + return [desc stringByReplacingOccurrencesOfString:@",\n" withString:@"\n" options:NSBackwardsSearch range:[desc rangeOfString:@",\n" options:NSBackwardsSearch]]; +} + +@end + + +@implementation NSSet (UnicodeReadable) + +- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level { + NSMutableString *desc = [NSMutableString string]; + + NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level]; + for (NSUInteger i = 0; i < level; i++) { + [tabString appendString:@"\t"]; + } + + NSString *tab = @""; + if (level > 0) { + tab = tabString; + } + [desc appendString:@"{(\n"]; + + for (id obj in self) { + if ([obj isKindOfClass:[NSDictionary class]] + || [obj isKindOfClass:[NSArray class]] + || [obj isKindOfClass:[NSSet class]]) { + NSString *str = [((NSDictionary *)obj) descriptionWithLocale:locale indent:level + 1]; + [desc appendFormat:@"%@\t%@,\n", tab, str]; + } else if ([obj isKindOfClass:[NSString class]]) { + [desc appendFormat:@"%@\t\"%@\",\n", tab, obj]; + } else if ([obj isKindOfClass:[NSData class]]) { + // 如果是NSData类型,尝试去解析结果,以打印出可阅读的数据 + NSError *error = nil; + NSObject *result = [NSJSONSerialization JSONObjectWithData:obj + options:NSJSONReadingMutableContainers + error:&error]; + // 解析成功 + if (error == nil && result != nil) { + if ([result isKindOfClass:[NSDictionary class]] + || [result isKindOfClass:[NSArray class]] + || [result isKindOfClass:[NSSet class]]) { + NSString *str = [((NSDictionary *)result) descriptionWithLocale:locale indent:level + 1]; + [desc appendFormat:@"%@\t%@,\n", tab, str]; + } else if ([obj isKindOfClass:[NSString class]]) { + [desc appendFormat:@"%@\t\"%@\",\n", tab, result]; + } + } else { + @try { + NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding]; + if (str != nil) { + [desc appendFormat:@"%@\t\"%@\",\n", tab, str]; + } else { + [desc appendFormat:@"%@\t%@,\n", tab, obj]; + } + } + @catch (NSException *exception) { + [desc appendFormat:@"%@\t%@,\n", tab, obj]; + } + } + } else { + [desc appendFormat:@"%@\t%@,\n", tab, obj]; + } + } + + [desc appendFormat:@"%@)}", tab]; + + return [desc stringByReplacingOccurrencesOfString:@",\n" withString:@"\n" options:NSBackwardsSearch range:[desc rangeOfString:@",\n" options:NSBackwardsSearch]]; +} + +@end + +#endif + +#endif