NSLog打印技巧

  • 打印包含中文的数组 和 字典
#import "NSArray+Log.h"

@implementation NSArray (Log)

// 模拟 NSArray 重新输出 用的时候拖进来用 不用的时候干掉
- (NSString *)descriptionWithLocale:(id)locale{
    NSMutableString *strM = [NSMutableString stringWithString:@"(\n"];
    // 遍历数组的内容 然后按照顺序拼接我们的字符串
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [strM appendFormat:@"\t%@,\n",obj];
    }];
    [strM appendFormat:@")\n"];
    return strM;
}

@end


@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale{
    NSMutableString *strM = [NSMutableString stringWithString:@"(\n"];
    // 遍历数组的内容 然后按照顺序拼接我们的字符串
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        // \t的意思就是对齐
        [strM appendFormat:@"\t%@ = %@,\n",key, obj];
    }];
    [strM appendFormat:@")\n"];
    return strM;
}

@end
  • 打印模型
+ (instancetype)personWithDict:(NSDictionary *)dict{
    id obj = [[self alloc] init];
    [obj setValuesForKeysWithDictionary:dict];
    return obj;
}

- (NSString *)description{
    return [NSString stringWithFormat:@"<%@:%p>{name:%@,age:%@}",[self class],self,self.name,self.age];
}

@end

你可能感兴趣的:(NSLog打印技巧)