编写高质量代码的52个有效方法

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

2018-09-04  本文已影响14人  SkyMing一C

description基本概念

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

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

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

description重写的方法
//.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
//.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的区别在于:
 person -> description: <SKYPerson: 0x604000022940,{
    address = "\U9ed1\U90fd";
    name = "\U738b\U5e3d\U5e3d";
}>
(lldb) po person
debugDescription: <SKYPerson: 0x604000022940,{
    address = "\U9ed1\U90fd";
    name = "\U738b\U5e3d\U5e3d";
}>

description陷阱

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

- (NSString *)description {

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

}
- (NSString *)description {

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

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

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

上一篇下一篇

猜你喜欢

热点阅读