• 打印日志显示中文数据


     找了些资料, 自己整理了一下, 代码如下:

    #import <Foundation/Foundation.h>
    
    @interface NSArray (Foundation_Log)
    
    
    @end
    #import "NSArray+Foundation_Log.h"
    
    #import <objc/runtime.h>
    
    #import <objc/message.h>
    
    @implementation NSArray (Foundation_Log)
     
    #if DEBUG
    
    - (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:@"	"];
        }
    
        NSString *tab = @"";
        if (level > 0) {
    
            tab = tabString;
        }
        [desc appendString:@"	(
    "];
    
        for (id obj in self) {
    
            if (![obj respondsToSelector:@selector(description)]) {
                continue;
            }
    
            if ([obj isKindOfClass:[NSDictionary class]]
    
                || [obj isKindOfClass:[NSArray class]]
    
                || [obj isKindOfClass:[NSSet class]]) {
    
                NSString *str = [((NSDictionary *)obj) descriptionWithLocale:locale indent:level + 1];
    
                [desc appendFormat:@"%@	%@,
    ", tab, str];
    
            } else if ([obj isKindOfClass:[NSString class]]) {
    
                [desc appendFormat:@"%@	"%@",
    ", 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:@"%@	%@,
    ", tab, str];
    
                    } else if ([obj isKindOfClass:[NSString class]]) {
    
                        [desc appendFormat:@"%@	"%@",
    ", tab, result];
                    }
                } else {
                    @try {
    NSString
    *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
    if (str != nil) { [desc appendFormat:@"%@ "%@", ", tab, str]; } else { [desc appendFormat:@"%@ %@, ", tab, obj]; } } @catch (NSException *exception) { [desc appendFormat:@"%@ %@, ", tab, obj]; } } } else { @try { [desc appendFormat:@"%@ %@, ", tab, obj]; } @catch (NSException *exception) { } @finally { } } } [desc appendFormat:@"%@)", tab]; return desc; } #endif @end
    @implementation NSDictionary (Foundation_Log)
    
    #if DEBUG
    
    - (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:@" "]; } NSString *tab = @""; if (level > 0) { tab = tabString; } [desc appendString:@" { "]; // 遍历数组,self就是当前的数组 for (id key in self.allKeys) { id obj = [self objectForKey:key]; if (![obj respondsToSelector:@selector(description)]) { continue; } if ([obj isKindOfClass:[NSString class]]) { [desc appendFormat:@"%@ %@ = "%@", ", tab, key, obj]; } else if ([obj isKindOfClass:[NSArray class]] || [obj isKindOfClass:[NSDictionary class]] || [obj isKindOfClass:[NSSet class]]) { [desc appendFormat:@"%@ %@ = %@, ", 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:@"%@ %@ = %@, ", tab, key, str]; } else if ([obj isKindOfClass:[NSString class]]) { [desc appendFormat:@"%@ %@ = "%@", ", tab, key, result]; } } else { @try { NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding]; if (str != nil) { [desc appendFormat:@"%@ %@ = "%@", ", tab, key, str]; } else { [desc appendFormat:@"%@ %@ = %@, ", tab, key, obj]; } } @catch (NSException *exception) { [desc appendFormat:@"%@ %@ = %@, ", tab, key, obj]; } } } else { @try { [desc appendFormat:@"%@ %@ = %@, ", tab, key, obj]; } @catch (NSException *exception) { } @finally { } } } [desc appendFormat:@"%@}", tab]; return desc; } #endif @end
    @implementation NSSet (Foundation_Log)
    
    #if DEBUG
    
    - (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:@"	"];
        }
    
        NSString *tab = @"	";
    
        if (level > 0) {
    
            tab = tabString;
        }
    [desc appendString:
    @" {( "];
    for (id obj in self) { if (![obj respondsToSelector:@selector(description)]) { continue; }
    if ([obj isKindOfClass:[NSDictionary class]] || [obj isKindOfClass:[NSArray class]] || [obj isKindOfClass:[NSSet class]]) { NSString *str = [((NSDictionary *)obj) descriptionWithLocale:locale indent:level + 1]; [desc appendFormat:@"%@ %@, ", tab, str]; } else if ([obj isKindOfClass:[NSString class]]) { [desc appendFormat:@"%@ "%@", ", 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:@"%@ %@, ", tab, str]; } else if ([obj isKindOfClass:[NSString class]]) { [desc appendFormat:@"%@ "%@", ", tab, result]; } } else { @try { NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding]; if (str != nil) { [desc appendFormat:@"%@ "%@", ", tab, str]; } else { [desc appendFormat:@"%@ %@, ", tab, obj]; } } @catch (NSException *exception) { [desc appendFormat:@"%@ %@, ", tab, obj]; } } } else { @try { [desc appendFormat:@"%@ %@, ", tab, obj]; } @catch (NSException *exception) { } @finally { } } } [desc appendFormat:@"%@)}", tab]; return desc; } #endif @end
    @implementation NSObject (Foundation_Log)
    
    - (NSString *)description {
    
        NSMutableDictionary *debugInfos = [NSMutableDictionary dictionary];
    
        unsigned int count = 0;
    
        Ivar *ivars = class_copyIvarList(self.class, &count);
    
        for (unsigned int i = 0; i < count; ++i) {
    
            Ivar ivar = ivars[i];
    
            const char *name = ivar_getName(ivar);
    
            NSString *ivarName = [NSString stringWithUTF8String:name];
    
     
    
            NSString *propertyName = ivarName;
    
            if ([propertyName hasPrefix:@"_"]) {
    
                propertyName = [propertyName substringFromIndex:1];
            }
    
            const char *type = ivar_getTypeEncoding(ivar);
    
            NSString *typeEncoding = [NSString stringWithUTF8String:type];
    
            id value = nil;
    
            // object
            if ([typeEncoding rangeOfString:@"@"].location != NSNotFound) {
    
                value =  ((id (*)(id, SEL))objc_msgSend)((id)self, NSSelectorFromString(propertyName));
    
            } else if ([typeEncoding rangeOfString:@"*"].location != NSNotFound) {
    
                char *v = ((char * (*)(id, SEL))objc_msgSend)((id)self, NSSelectorFromString(propertyName));
    
                value = [NSString stringWithUTF8String:v];
    
            } else if ([typeEncoding rangeOfString:@"#"].location != NSNotFound) {
    
                value = propertyName;
    
            } else if ([typeEncoding rangeOfString:@"^"].location != NSNotFound) {
    
                value = @"基本C指针";
    
            } else {
                @try {
                    value = [self valueForKey:propertyName];
                }
    
                @catch (NSException *exception) {
                }
                @finally {
                }
            }
    
            value = value == nil ? @"<nil>" : value;
    
            [debugInfos setValue:value forKey:propertyName];
    
        }
    free(ivars);
    return debugInfos.description; } - (NSString *)debugDescription { NSMutableDictionary *debugInfos = [NSMutableDictionary dictionary]; unsigned int count = 0; Ivar *ivars = class_copyIvarList(self.class, &count); for (unsigned int i = 0; i < count; ++i) { Ivar ivar = ivars[i]; const char *name = ivar_getName(ivar); NSString *ivarName = [NSString stringWithUTF8String:name]; } free(ivars); return debugInfos.debugDescription; } @end
  • 相关阅读:
    SQLSERVER查询某张表哪些字段包含某关键字
    SQLSERVER初始化机构path
    Golang的ORM框架之gorm
    Golang的web框架之Gin
    初识Go逆向
    GIT 常用命令
    mac上系统偏好里无法停止mysql
    js正则高级用法: 分组和断言
    Java强软弱虚四种引用的使用场景
    两数之和
  • 原文地址:https://www.cnblogs.com/Milo-CTO/p/6433835.html
Copyright © 2020-2023  润新知