如果在 console 中遇到了 NSArray
、NSDictionary
打印中文亂碼問題光涂,解決方法之一是創(chuàng)建 NSArray+Extension
和 NSDictionary+Extension
兩個Category 分類日杈。
?? Xcode 8.0 之后使用 NSLog
打印時可能會遇到該問題遣铝。
NSArray+Extension
-(NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *msr = [NSMutableString string];
[msr appendString:@"["];
for (id obj in self) {
[msr appendFormat:@"\n\t%@,",obj];
}
//去掉最后一個逗號(,)
if ([msr hasSuffix:@","]) {
NSString *str = [msr substringToIndex:msr.length - 1];
msr = [NSMutableString stringWithString:str];
}
[msr appendString:@"\n]"];
return msr;
}
NSDictionary+Extension
-(NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *msr = [NSMutableString string];
[msr appendString:@"{"];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[msr appendFormat:@"\n\t%@ = %@,",key,obj];
}];
//去掉最后一個逗號(,)
if ([msr hasSuffix:@","]) {
NSString *str = [msr substringToIndex:msr.length - 1];
msr = [NSMutableString stringWithString:str];
}
[msr appendString:@"\n}"];
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;
}