• Xcode控制台输出中文


    创建一个.m文件,然后将一下代码加入.m文件中即可实现控制台输出中文,具体代码如下:

    #ifndef Release
    
    @implementation NSSet(Log)
    
    - (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 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]]) { // if is NSData,try parse 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 { [desc appendFormat:@"%@ %@, ", tab, obj]; } } [desc appendFormat:@"%@)}", tab]; return desc; } @end @implementation NSArray (Log) - (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 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]]) { 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 { [desc appendFormat:@"%@ %@, ", tab, obj]; } } [desc appendFormat:@"%@)", tab]; return desc; } @end @implementation NSDictionary (Log) - (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:@" { "]; // Through array, self is array for (id key in self.allKeys) { id obj = [self objectForKey:key]; 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]]) { 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 { [desc appendFormat:@"%@ %@ = %@, ", tab, key, obj]; } } [desc appendFormat:@"%@}", tab]; return desc; } @end #endif

    以上代码实现了NSSet/NSDictionry/NSArray类型的展示。

  • 相关阅读:
    网络流之对偶图转最短路
    BZOJ5418 NOI2018屠龙勇士EXCRT
    BZOJ1951 [Sdoi2010]古代猪文 NOIP数论大杂烩
    中国剩余定理及EX及单层EXLucas定理讲解
    网络流24题之负载平衡问题
    输入一个url到浏览器页面展示都经历了哪些过程
    前端部署dist包到服务器
    img标签显示 base64格式的 图片
    字符串用react 用sha256加密
    前端下载证书文件cer用后端返回的加密数据
  • 原文地址:https://www.cnblogs.com/sandyzhang/p/10077717.html
Copyright © 2020-2023  润新知