美文网首页
NSLog打印技巧

NSLog打印技巧

作者: 952625a28d0d | 来源:发表于2017-02-08 23:11 被阅读119次
  • 打印包含中文的数组 和 字典
#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打印技巧

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