美文网首页
Xcode打印信息中文乱码解决

Xcode打印信息中文乱码解决

作者: 通哥 | 来源:发表于2018-09-17 18:52 被阅读0次

    新建LogCategory.h 和 LogCategory.m文件


    在.h文件中放入

    #import <Foundation/Foundation.h>
    
    // 如果需要关掉兼容中文的打印,把下面的宏注释掉即可
    #define UseLogChinese
    
    
    @interface NSArray (Log)
    
    @end
    
    @interface NSDictionary (Log)
    
    @end
    
    @interface NSSet (Log)
    
    @end
    

    在.m文件中放入

    #import "LogCategory.h"
    
    static NSString * const kLxArrayBegin = @"[";
    static NSString * const kLxArrayEnd = @"]";
    static NSString * const kLxDictionaryBegin = @"{";
    static NSString * const kLxDictionaryEnd = @"}";
    static NSString * const kLxSetBegin = @"{(";
    static NSString * const kLxSetEnd = @")}";
    
    @implementation NSArray (Log)
    
    #ifdef UseLogChinese
    
    - (NSString *)debugDescription {
        return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
    }
    
    - (NSString *)description {
        return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
    }
    
    - (NSString *)descriptionWithLocale:(id)locale {
        NSMutableString * string = [NSMutableString string];
        [string appendFormat:@"%@\n", kLxArrayBegin];
        NSUInteger count = self.count;
        [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString * temp = nil;
            if ([obj respondsToSelector:@selector(descriptionWithLocale:)]) {
                temp = [obj performSelector:@selector(descriptionWithLocale:) withObject:locale];
                temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
            } else {
                temp = [obj performSelector:@selector(description) withObject:nil];
                if ([obj isKindOfClass:[NSString class]]) {
                    temp = [NSString stringWithFormat:@"\"%@\"", temp];
                }
            }
            [string appendFormat:@"\t%@", temp];
            if (idx+1 != count) {
                [string appendString:@","];
            }
            [string appendString:@"\n"];
        }];
        [string appendString:kLxArrayEnd];
        return string;
    }
    
    #endif
    
    @end
    
    @implementation NSDictionary (Log)
    
    #ifdef UseLogChinese
    
    - (NSString *)debugDescription {
        return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
    }
    - (NSString *)description {
        return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
    }
    - (NSString *)descriptionWithLocale:(id)locale {
        NSMutableString * string = [NSMutableString string];
        [string appendFormat:@"%@\n", kLxDictionaryBegin];
        NSUInteger count = self.allKeys.count;
        NSArray * allKeys = self.allKeys;
        BOOL canCom = YES;
        for (id temp in allKeys) {
            if ([temp respondsToSelector:@selector(compare:)] == NO) {
                canCom = NO;
                break;
            }
        }
        if (canCom) {
            allKeys = [self.allKeys sortedArrayUsingSelector:@selector(compare:)];
        }
        for (id key in allKeys) {
            NSInteger index = [self.allKeys indexOfObject:key];
            id value = [self objectForKey:key];
            NSString * temp = nil;
            if ([value respondsToSelector:@selector(descriptionWithLocale:)]) {
                temp = [value performSelector:@selector(descriptionWithLocale:) withObject:locale];
                temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
            } else {
                temp = [value performSelector:@selector(description) withObject:nil];
                if ([value isKindOfClass:[NSString class]]) {
                    temp = [NSString stringWithFormat:@"\"%@\"", temp];
                }
            }
            [string appendFormat:@"\t%@ = %@", key, temp];
            if (index+1 != count) {
                [string appendString:@";"];
            }
            [string appendString:@"\n"];
        }
        [string appendString:kLxDictionaryEnd];
        return string;
        
    }
    
    #endif
    @end
    
    
    
    @implementation NSSet (Log)
    
    #ifdef UseLogChinese
    
    - (NSString *)debugDescription {
        return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
    }
    - (NSString *)description {
        return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
    }
    - (NSString *)descriptionWithLocale:(id)locale {
        NSMutableString * string = [NSMutableString string];
        [string appendFormat:@"%@\n", kLxSetBegin];
        NSUInteger count = self.count;
        __block NSInteger idx = 0;
        [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSString * temp = nil;
            if ([obj respondsToSelector:@selector(descriptionWithLocale:)]) {
                temp = [obj performSelector:@selector(descriptionWithLocale:) withObject:locale];
                temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
            } else {
                temp = [obj performSelector:@selector(description) withObject:nil];
                if ([obj isKindOfClass:[NSString class]]) {
                    temp = [NSString stringWithFormat:@"\"%@\"", temp];
                }
            }
            [string appendFormat:@"\t%@", temp];
            if (idx+1 != count) {
                [string appendString:@","];
            }
            [string appendString:@"\n"];
            idx += 1;
        }];
        [string appendString:kLxSetEnd];
        return string;
        
    }
    
    #endif
    @end
    

    相关文章

      网友评论

          本文标题:Xcode打印信息中文乱码解决

          本文链接:https://www.haomeiwen.com/subject/vdzrnftx.html