美文网首页编写高质量代码的52个有效方法
52个有效方法(17) - 实现description方法

52个有效方法(17) - 实现description方法

作者: SkyMing一C | 来源:发表于2018-09-04 16:20 被阅读14次

description基本概念

  1. NSLog(@"%@", objectA);这会自动调用objectA的description方法来输出ObjectA的描述信息。

  2. description方法默认返回对象的描述信息(默认实现是返回类名和对象的内存地址)。

  3. description方法是基类NSObject 所带的方法,因为其默认实现是返回类名和对象的内存地址, 这样的话,使用NSLog输出OC对象,意义就不是很大,因为我们并不关心对象的内存地址,比较关心的是对象内部的一些成变量的值。因此,会经常重写description方法,覆盖description方法 的默认实现。

description重写的方法
  • 将对象转成NSDictionary的分类
//.h
#import <Foundation/Foundation.h>

@interface NSObject (SKYExtension)
- (NSDictionary*)ag_toDictionary;
@end
//.m
#import "NSObject+SKYExtension.h"
#import <objc/runtime.h>
#import <objc/message.h>

@implementation NSObject (SKYExtension)
- (void)ag_enumeratePropertiesUsingBlock:(void (^)(objc_property_t property, BOOL *stop))block {
    Class cls = self.class;
    BOOL stop = NO;
    
    while (!stop && ![cls isEqual:NSObject.class]) {
        unsigned count = 0;
        objc_property_t *properties = class_copyPropertyList(cls, &count);
        
        cls = cls.superclass;
        if (properties == NULL) continue;
        for (unsigned i = 0; i < count; i++) {
            block(properties[i], &stop);
            if (stop) break;
        }
        free(properties);
    }
}
- (NSDictionary*)ag_toDictionary{
    NSMutableDictionary *propertiesInfo = [NSMutableDictionary dictionary];
    [self ag_enumeratePropertiesUsingBlock:^(objc_property_t property, BOOL *stop) {
        NSString *key = @(property_getName(property));
        id value = [self valueForKey:key];
        if (value) {
            propertiesInfo[key] = value;
        }else{
            propertiesInfo[key] = [NSNull null];
        }
    }];
    return propertiesInfo;
}
@end
  • 自定义SKYPerson类
//.h
#import <Foundation/Foundation.h>

@interface SKYPerson : NSObject
@property (nonatomic, copy, readonly) NSString * name;
@property (nonatomic, copy, readonly) NSString * address;

- (id)initWithName:(NSString *)name address:(NSString *)address;
@end

//.m
#import "SKYPerson.h"
#import "NSObject+SKYExtension.h"

@implementation SKYPerson
- (id)initWithName:(NSString *)name address:(NSString *)address{
    if ((self = [super init])){
        _name = [name copy];
        _address = [address copy];
    }
    return self;
}

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

-(NSString *)debugDescription{
    return [NSString stringWithFormat:@"debugDescription: <%@: %p,%@>", [self class],self,[self ag_toDictionary]];
}
@end
  • 使用
    SKYPerson *person = [[SKYPerson alloc] initWithName:@"王帽帽" address:@"黑都"];
    NSLog(@"person -> %@", person);
//breakpoint here

对于description和debugDescription的区别在于:
  • description:从控制台输出NSLog
 person -> description: <SKYPerson: 0x604000022940,{
    address = "\U9ed1\U90fd";
    name = "\U738b\U5e3d\U5e3d";
}>
  • debugDescription:通过断点po打印(在NSObject类的默认实现中,此方法只是直接调用了description
(lldb) po person
debugDescription: <SKYPerson: 0x604000022940,{
    address = "\U9ed1\U90fd";
    name = "\U738b\U5e3d\U5e3d";
}>

description陷阱

不要在description方法中同时使用%@self,下面的写法是错误的。

- (NSString *)description {

return [NSString stringWithFormat:@"%@", self];

}
  • 同时使用了%@self,代表要调用selfdescription方法,因此最终会导致程序陷入死循环,循 环调用description方法。

  • [NSString stringWithFormat:@“%@”, self]; 使用它时,循坏调用,导致系统会发生运行时错误。

  • 当该方法使用NSLog(“%@”,self)时候, 系统做了相关的优化,循坏调用3次后就会自动退出。

  • 正确写法:

- (NSString *)description {

return [NSString stringWithFormat:@"%p", self];

}
要点
  1. 实现description方法返回一个有意义的字符串,用以描述该实例。 我们可以利用运行时先将对象转成NSDictionary对象,再调用NSDictionary的description方法打印出对象的信息。当然,我们直接重写description,打印自定义的信息和格式也是非常好的。

  2. 若想在调试时打印出更详尽的对象描述信息,则应实现debugDescription方法。

相关文章

网友评论

    本文标题:52个有效方法(17) - 实现description方法

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