Objective-Cプログラミング:NSArray、NSDictionary印刷中国語文字化けし問題

1969 ワード

NSArray、NSDictionary印刷中国語文字化けし問題
コンソールでNSArray、NSDictionaryの中国語文字化けしの問題に遭遇した場合、解決策の1つはNSArray+ExtensionNSDictionary+Extensionの2つのCategory分類を作成することです.
⚠️ Xcode 8.0以降NSLogで印刷すると、この問題が発生します.
NSArray+Extension
-(NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *msr = [NSMutableString string];
    [msr appendString:@"["];
    for (id obj in self) {
        [msr appendFormat:@"
\t%@,",obj]; } // (,) if ([msr hasSuffix:@","]) { NSString *str = [msr substringToIndex:msr.length - 1]; msr = [NSMutableString stringWithString:str]; } [msr appendString:@"
]"]; return msr; }

NSDictionary+Extension
-(NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *msr = [NSMutableString string];
    [msr appendString:@"{"];
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [msr appendFormat:@"
\t%@ = %@,",key,obj]; }]; // (,) if ([msr hasSuffix:@","]) { NSString *str = [msr substringToIndex:msr.length - 1]; msr = [NSMutableString stringWithString:str]; } [msr appendString:@"
}"]; return msr; }

NSDictionaryのvalueが空のcrash
/**
   value     crash

 @return     NSDictionary
 */
//.h
- (NSDictionary *)deleteAllNULLValue;

//.m
- (NSDictionary *)deleteAllNULLValue {
    NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init];
    for (NSString *keyStr in self.allKeys) {
        if ([[self objectForKey:keyStr] isEqual:[NSNull null]]) {
            [mutableDic setObject:@"null" forKey:keyStr];
        }
        else{
            [mutableDic setObject:[self objectForKey:keyStr] forKey:keyStr];
        }
    }
    return mutableDic;
}

その他の方法
  • GitHub:HYBUnicodeReadable