接上文,本文主要针对YYModel中一些使用的runtime的方法以及其他代码片段提供一个指南:

继续使用上文的类YYMessage,如下:

@interface YYMessage : NSObject
@property (nonatomic, assign) uint64_t messageId;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, strong) NSDate *time;
@property (nonatomic ,copy)   NSString *name;
@end

@implementation YYMessage
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"messageId":@[@"id", @"ID", @"mes_id"],
             @"time":@"t",
             @"name":@"user.name"
             };
}
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
    uint64_t timestamp = [dic unsignedLongLongValueForKey:@"t" default:0];
    self.time = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];
    return YES;
}
- (void)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
    dic[@"t"] = @([self.time timeIntervalSince1970] * 1000).description;
}
@end

Class

class

类对象是什么?

YYMessage  instance class is YYMessage

    Class cls = [self class];

class_isMetaClass

是否是元类?

YYMessage  instance isn't meta class

    _isMeta = class_isMetaClass(cls);		//_isMeta = NO;

class_getName

获取类名

YYMessage instance name is "YYMessage"

	_name = class_getName(cls);		//_name is "YYMessage"

objc_getMetaClass

获取元类对象

YYMessage instance metaCls is YYMessage