利用Runtime動(dòng)態(tài)綁定Model屬性

利用Runtime動(dòng)態(tài)綁定Model屬性

大家如果在開(kāi)發(fā)中使用過(guò)從網(wǎng)絡(luò)獲取JSON數(shù)據(jù),那么一定對(duì)model.value = [dictionary objectForKey:@"key"]很熟悉,相信大多數(shù)剛開(kāi)始學(xué)習(xí)iOS網(wǎng)絡(luò)開(kāi)發(fā)的人都是使用類似以上這句代碼將解析為NSDictionary對(duì)象的JSON數(shù)據(jù)綁定到Model上的??墒侨绻绦蛑杏泻芏郙odel或者M(jìn)odel中有很多屬性,這么做就會(huì)加大很多工作量,那么有沒(méi)有什么簡(jiǎn)單的方法解決這個(gè)問(wèn)題呢?答案就是Runtime技術(shù)!

準(zhǔn)備工作

首先,建立一個(gè)Model類,我把它命名為KCModel,它是應(yīng)用中所有Model的父類,應(yīng)用不能直接使用該類,定義一個(gè)協(xié)議(面向接口編程),Model實(shí)現(xiàn)該協(xié)議,我命名為KCModelAutoBinding,協(xié)議聲明的方法有:

+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary;
+ (NSDictionary *)dictionaryKeyPathByPropertyKey;
- (void)autoBindingWithDictionary:(NSDictionary *)dictionary;

注意其中有兩個(gè)個(gè)類方法,說(shuō)明如下:

第一個(gè)不說(shuō)了;
+ dictionaryKeyPathByPropertyKey 屬性映射的值在 dictionary 中的位置,比如 myName 屬性映射 dictionary[@"name"] ,則返回 @{@"myName" : @"name"} ,而如果是多層關(guān)系,比如映射 dictionary[@"data"][@"name"] ,則返回 @{@"myName" : @"data.name"};
- autoBindingWithDictionary:dictionary 綁定到Model。

獲取Model所有屬性

在Runtime中有個(gè)函數(shù)可以獲取某個(gè)類的所有屬性:

class_copyPropertyList(Class cls, unsigned int *outCount)

這是一個(gè)C語(yǔ)言函數(shù),返回的值是objc_property_t的指針(代表一個(gè)數(shù)組)。
需要注意的是這個(gè)函數(shù)只能獲取到當(dāng)前類的屬性,而不能獲取到父類的屬性,我們可以使用遞歸的方法獲取到包含父類在內(nèi)的所有屬性。

以上我們獲得到了objc_property_t的數(shù)組,每個(gè)objc_property_t都代表一個(gè)屬性,我們可以使用以下方法得到屬性名:

property_getName(objc_property_t property)

要想得到更多的信息則需要它了:

property_getAttributes(objc_property_t property)

這個(gè)函數(shù)返回了一段char數(shù)組字符串給我們,有屬性的各種信息,但我們現(xiàn)在只需要一個(gè)信息,那就是屬性的類型。
來(lái)看Apple的Runtime指南:

You can use the property_getAttributes function to discover the name, the @encode type string of a property, and other attributes of the property.

The string starts with a T followed by the @encode type and a comma, and finishes with a V followed by the name of the backing instance variable.

也就是說(shuō),返回的字符串是以T開(kāi)頭,后面跟屬性類型等各種信息,信息之間用,隔開(kāi)。通過(guò)這些我們就可以得到屬性的類型了。
我們可以新建一個(gè)類來(lái)解析并儲(chǔ)存屬性的這些信息,我把它命名為KCModelProperty。在KCModel中,我將所有屬性信息用一個(gè)key為屬性名,valueKCModelProperty對(duì)象的NSDictionary儲(chǔ)存,方便使用。

獲取屬性映射的值

方法很簡(jiǎn)單,將屬性名作為key得到屬性映射的值在 dictionary 中的位置keyPath,不要問(wèn)我怎么獲得,這就是之前提到的類方法dictionaryKeyPathByPropertyKey的作用。

注意:如果屬性是自定義類型,只需要滿足實(shí)現(xiàn)了之前定義的KCModelAutoBinding協(xié)議,那么就可以通過(guò)遞歸的方式綁定該屬性。

使用KVC賦值

以上我們得到了dictionary所在keyPath位置的值,那么怎么把它賦值給屬性呢?答案是

Class NSClassFromString(NSString *aClassName);

我們通過(guò)這個(gè)方法可以得到屬性的類,然后就可以開(kāi)始賦值了。
注意:類分為兩種,一種是系統(tǒng)定義好的類,另一種是自定義的類——其他Model對(duì)象。因?yàn)槎鄶?shù)情況下通過(guò)解析JSON得到的NSDictionary對(duì)象(如使用AFNetworking)里儲(chǔ)存的都是系統(tǒng)的類,如:NSIntegerNSArray等,所以如果是第一種類,只要與dictionary中的值類型一樣就可以直接用它來(lái)賦值了,但是第二種類就需要使用其他方法賦值了,方法就是最前面提到的類方法modelWithDictionary:,通過(guò)這個(gè)方法得到其他Model對(duì)象,再進(jìn)行賦值。
賦值方法就是Key-Value Coding技術(shù)的setValue:forKey:。

</br>
大功告成。

思路說(shuō)起來(lái)很簡(jiǎn)單,實(shí)際動(dòng)手又是另外一回事。
</br>

附上我的代碼:

//KCModel.h

#import <Foundation/Foundation.h>

//快速定義與類相同名稱的協(xié)議(數(shù)組元素類型標(biāo)記)
#define KC_ARRAY_TYPE(VAL) \
@protocol VAL <NSObject> \
@end

@protocol KCModelAutoBinding <NSObject>

+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary;
+ (NSArray *)modelsWithArray:(NSArray *)array;
- (void)autoBindingWithDictionary:(NSDictionary *)dictionary;

@end

@interface KCModel : NSObject <KCModelAutoBinding>

+ (NSDictionary *)dictionaryKeyPathByPropertyKey;

@end
//KCModel.m

static id KCTransformNormalValueForClass(id val, NSString *className) {
    id ret = val;
    
    Class valClass = [val class];
    Class cls = nil;
    if (className.length > 0) {
        cls = NSClassFromString(className);
    }
    
    if (!cls || !valClass) {
        ret = nil;
    } else if (![cls isSubclassOfClass:[val class]] && ![valClass isSubclassOfClass:cls]) {
        ret = nil;
    }
    
    return ret;
}

@implementation KCModel

#pragma mark -- KCItemAutoBinding
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary
{
    id<KCModelAutoBinding> model = [[self class] new];
    [model autoBindingWithDictionary:dictionary];
    
    return model;
}

+ (NSArray *)modelsWithArray:(NSArray *)array
{
    NSMutableArray *models = @[].mutableCopy;
    for (NSDictionary *dict in array) {
        [models addObject:[self modelWithDictionary:dict]];
    }
    
    return [NSArray arrayWithArray:models];
}

- (void)autoBindingWithDictionary:(NSDictionary *)dictionary
{
    NSDictionary *properties = [self.class propertyInfos];
    NSDictionary *dictionaryKeyPathByPropertyKey = [self.class dictionaryKeyPathByPropertyKey];
    
    for (KCModelProperty *property in [properties allValues]) {
        KCModelPropertyType propertyType = property.propertyType;
        NSString *propertyName = property.propertyName;
        NSString *propertyClassName = property.propertyClassName;
        NSString *propertyKeyPath = propertyName;
        
        //獲取屬性映射的dictionary內(nèi)容位置
        if ([dictionaryKeyPathByPropertyKey objectForKey:propertyName]) {
            propertyKeyPath = [dictionaryKeyPathByPropertyKey objectForKey:propertyName];
        }
        
        id value = [dictionary kc_valueForKeyPath:propertyKeyPath]; //從dictionary中得到映射的值
        
        if (value == nil || value == [NSNull null]) {
            continue;
        }
        
        Class propertyClass = nil;
        if (propertyClassName.length > 0) {  //非系統(tǒng)自帶對(duì)象
            propertyClass = NSClassFromString(propertyClassName);
        }
        
        //轉(zhuǎn)換value
        switch (propertyType) {
            //基本數(shù)字類型
            case KCModelPropertyTypeInt:
            case KCModelPropertyTypeFloat:
            case KCModelPropertyTypeDouble:
            case KCModelPropertyTypeBool:
            case KCModelPropertyTypeNumber:{
                if ([value isKindOfClass:[NSString class]]) {
                    NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
                    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                    value = [numberFormatter numberFromString:value];
                }else{
                    value = KCTransformNormalValueForClass(value, NSStringFromClass([NSNumber class]));
                }
            }
                break;
            case KCModelPropertyTypeChar:{
                if ([value isKindOfClass:[NSString class]]) {
                    char firstCharacter = [value characterAtIndex:0];
                    value = [NSNumber numberWithChar:firstCharacter];
                } else {
                    value = KCTransformNormalValueForClass(value, NSStringFromClass([NSNumber class]));
                }
            }
                break;
            case KCModelPropertyTypeString:{
                if ([value isKindOfClass:[NSNumber class]]) {
                    value = [value stringValue];
                } else {
                    value = KCTransformNormalValueForClass(value, NSStringFromClass([NSString class]));
                }
            }
                break;
            case KCModelPropertyTypeData:{
                value = KCTransformNormalValueForClass(value, NSStringFromClass([NSData class]));
            }
                break;
            case KCModelPropertyTypeDate:{
                value = KCTransformNormalValueForClass(value, NSStringFromClass([NSDate class]));
            }
                break;
            case KCModelPropertyTypeAny:
                break;
            case KCModelPropertyTypeDictionary:{
                value = KCTransformNormalValueForClass(value, NSStringFromClass([NSDictionary class]));
            }
                break;
            case KCModelPropertyTypeMutableDictionary:{
                value = KCTransformNormalValueForClass(value, NSStringFromClass([NSDictionary class]));
                value = [value mutableCopy];
            }
                break;
            case KCModelPropertyTypeArray:{
                if (propertyClass && [propertyClass isSubclassOfClass:[KCModel class]]) {  //儲(chǔ)存KCItem子類對(duì)象的數(shù)組
                    value = [propertyClass itemsWithArray:value];
                }else{
                    value = KCTransformNormalValueForClass(value, NSStringFromClass([NSArray class]));
                }
            }
                break;
            case KCModelPropertyTypeMutableArray:{
                value = KCTransformNormalValueForClass(value, NSStringFromClass([NSArray class]));
                value = [value mutableCopy];
            }
                break;
            case KCModelPropertyTypeObject:
            case KCModelPropertyTypeModel:{
                if (propertyClass) {
                    if ([propertyClass conformsToProtocol:@protocol(KCModelAutoBinding)]     //屬性為實(shí)現(xiàn)了KCModelAutoBinding協(xié)議的對(duì)象
                        && [value isKindOfClass:[NSDictionary class]]) {
                        NSDictionary *oldValue = value;
                        value = [[propertyClass alloc] init];
                        [value autoBindingWithDictionary:oldValue];
                    }else{
                        value = KCTransformNormalValueForClass(value, propertyClassName);
                    }
                }
            }
                break;
        }
        
        //KVC
        if (value && value != [NSNull null]) {
            [self setValue:value forKey:propertyName];
        }
    }
}

#pragma mark -- Class method
+ (NSDictionary *)propertyInfos
{
    //獲取緩存數(shù)據(jù)
    NSDictionary *cachedInfos = objc_getAssociatedObject(self, _cmd);
    if (cachedInfos != nil) {
        return cachedInfos;
    }
    
    NSMutableDictionary *ret = [NSMutableDictionary dictionary];
    
    unsigned int propertyCount;
    objc_property_t *properties = class_copyPropertyList(self, &propertyCount); //獲取自身的所有屬性(c語(yǔ)言,*properties代表數(shù)組)
    Class superClass = class_getSuperclass(self);
    
    //獲取父類的所有屬性
    if (superClass && ![NSStringFromClass(superClass) isEqualToString:@"KCModel"]) {
        NSDictionary *superProperties = [superClass propertyInfos];  //遞歸
        [ret addEntriesFromDictionary:superProperties];
    }
    
    for (int i = 0; i < propertyCount; i++) {
        objc_property_t property = properties[i];   //獲取第i個(gè)屬性
        const char *propertyCharName = property_getName(property);  //獲取當(dāng)前屬性的名稱
        NSString *propertyName = @(propertyCharName);
        
        KCModelProperty *propertyInfo = [[KCModelProperty alloc] initWithPropertyName:propertyName objcProperty:property];
        [ret setValue:propertyInfo forKey:propertyName];
    }
    
    free(properties);
    
    //設(shè)置緩存數(shù)據(jù)
    objc_setAssociatedObject(self, @selector(propertyInfos), ret, OBJC_ASSOCIATION_COPY);
    
    return ret;
}

+ (NSDictionary *)dictionaryKeyPathByPropertyKey
{
    return [NSDictionary dictionaryWithObjects:[self propertyNames] forKeys:[self propertyNames]];
}

+ (NSArray *)propertyNames
{
    NSDictionary *ret = [self propertyInfos];
    return [ret allKeys];
}

@end
//KCModelProperty.h

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

typedef NS_ENUM(NSInteger, KCModelPropertyType) {
    KCModelPropertyTypeInt = 0,
    KCModelPropertyTypeFloat,
    KCModelPropertyTypeDouble,
    KCModelPropertyTypeBool,
    KCModelPropertyTypeChar,
    
    KCModelPropertyTypeString,
    KCModelPropertyTypeNumber,
    KCModelPropertyTypeData,
    KCModelPropertyTypeDate,
    KCModelPropertyTypeAny,
    
    KCModelPropertyTypeArray,
    KCModelPropertyTypeMutableArray,
    KCModelPropertyTypeDictionary,
    KCModelPropertyTypeMutableDictionary,
    KCModelPropertyTypeObject,
    KCModelPropertyTypeModel
};

@interface KCModelProperty : NSObject

@property (nonatomic, strong, readonly) NSString*   propertyClassName;
@property (nonatomic, strong, readonly) NSString*   propertyName;
@property (nonatomic, assign, readonly) KCModelPropertyType propertyType;

- (instancetype)initWithPropertyName:(NSString *)propertyName objcProperty:(objc_property_t)objcProperty;

@end
//KCModelProperty.m

#import "KCModelProperty.h"
#import "KCModel.h"

@implementation KCModelProperty

- (instancetype)initWithPropertyName:(NSString *)propertyName objcProperty:(objc_property_t)objcProperty
{
    if (self = [super init]) {
        _propertyName = propertyName;
        
        /*********************************************
         Apple "Objective-C Runtime Programming Guide":
            You can use the property_getAttributes function to discover the name, 
            the @encode type string of a property, and other attributes of the property.
            The string starts with a T followed by the @encode type and a comma, and finishes 
            with a V followed by the name of the backing instance variable.
        *********************************************/
        const char *attr = property_getAttributes(objcProperty);
        NSString *propertyAttributes = @(attr); //使用","隔開(kāi)的屬性描述字符串
        propertyAttributes = [propertyAttributes substringFromIndex:1]; //移除"T"
        
        NSArray *attributes = [propertyAttributes componentsSeparatedByString:@","]; //屬性描述數(shù)組
        
        NSString *typeAttr = attributes[0];  //屬性類型名稱
        const char *typeCharAttr = [typeAttr UTF8String];
        
        NSString *encodeCodeStr = [typeAttr substringToIndex:1];  //屬性類型
        const char *encodeCode = [encodeCodeStr UTF8String];
        const char typeEncoding = *encodeCode;
        
        //判斷類型
        switch (typeEncoding) {
            case 'i': // int
            case 's': // short
            case 'l': // long
            case 'q': // long long
            case 'I': // unsigned int
            case 'S': // unsigned short
            case 'L': // unsigned long
            case 'Q': // unsigned long long
                _propertyType = KCModelPropertyTypeInt;
                break;
            case 'f': // float
                _propertyType = KCModelPropertyTypeFloat;
                break;
            case 'd': // double
                _propertyType = KCModelPropertyTypeDouble;
                break;
            case 'B': // BOOL
                _propertyType = KCModelPropertyTypeBool;
                break;
            case 'c': // char
            case 'C': // unsigned char
                _propertyType = KCModelPropertyTypeChar;
                break;
            case '@':{ //object
                
                
                static const char arrayPrefix[] = "@\"NSArray<";  //NSArray,且遵循某個(gè)協(xié)議
                static const int arrayPrefixLen = sizeof(arrayPrefix) - 1;
                
                if (typeCharAttr[1] == '\0') {
                    // string is "@"
                    _propertyType = KCModelPropertyTypeAny;
                } else if (strncmp(typeCharAttr, arrayPrefix, arrayPrefixLen) == 0) {
                    /*******************
                        因?yàn)橹挥蠳SArray遵循某個(gè)協(xié)議才能被property_getAttributes()函數(shù)識(shí)別出來(lái),
                        以此為標(biāo)記表示這個(gè)數(shù)組存儲(chǔ)著以協(xié)議名為類名的Model對(duì)象
                     *******************/
                    _propertyType = KCModelPropertyTypeArray;
                    NSString *className = [[NSString alloc] initWithBytes:typeCharAttr + arrayPrefixLen
                                                                   length:strlen(typeCharAttr + arrayPrefixLen) - 2
                                                                 encoding:NSUTF8StringEncoding];
                    
                    Class propertyClass = NSClassFromString(className);
                    if (propertyClass) {
                        _propertyClassName = NSStringFromClass(propertyClass);
                    }
                } else if (strcmp(typeCharAttr, "@\"NSString\"") == 0) {
                    _propertyType = KCModelPropertyTypeString;
                } else if (strcmp(typeCharAttr, "@\"NSNumber\"") == 0) {
                    _propertyType = KCModelPropertyTypeNumber;
                } else if (strcmp(typeCharAttr, "@\"NSDate\"") == 0) {
                    _propertyType = KCModelPropertyTypeDate;
                } else if (strcmp(typeCharAttr, "@\"NSData\"") == 0) {
                    _propertyType = KCModelPropertyTypeData;
                } else if (strcmp(typeCharAttr, "@\"NSDictionary\"") == 0) {
                    _propertyType = KCModelPropertyTypeDictionary;
                } else if (strcmp(typeCharAttr, "@\"NSArray\"") == 0) {
                    _propertyType = KCModelPropertyTypeArray;
                } else if (strcmp(typeCharAttr, "@\"NSMutableArray\"") == 0){
                    _propertyType = KCModelPropertyTypeMutableArray;
                } else if (strcmp(typeCharAttr, "@\"NSMutableDictionary\"") == 0){
                    _propertyType = KCModelPropertyTypeMutableDictionary;
                }else {
                    _propertyType = KCModelPropertyTypeObject;
                    
                    Class propertyClass = nil;
                    if (typeAttr.length >= 3) {
                        NSString* className = [typeAttr substringWithRange:NSMakeRange(2, typeAttr.length-3)];
                        propertyClass = NSClassFromString(className);
                    }
                    
                    if (propertyClass) {
                        if ([propertyClass isSubclassOfClass:[KCModel class]]) {
                            _propertyType = KCModelPropertyTypeModel;
                        }
                        _propertyClassName = NSStringFromClass(propertyClass);
                    }
                    
                }
            }
                break;
            default:
                break;
        }
    }
    return self;
}

@end
//NSDictionary+KCModel.h

#import <Foundation/Foundation.h>

@interface NSDictionary (KCModel)

- (id)kc_valueForKeyPath:(NSString *)keyPath;

@end
//NSDictionary+KCModel.m

@implementation NSDictionary (KCModel)

- (id)kc_valueForKeyPath:(NSString *)keyPath
{
    NSArray *components = [keyPath componentsSeparatedByString:@"."];
    
    id ret = self;
    for (NSString *component in components) {
        if (ret == nil || ret == [NSNull null] || ![ret isKindOfClass:[NSDictionary class]]) {
            break;
        }
        ret = ret[component];
    }
    return ret;
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評(píng)論 0 9
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • 概述 ? iOS源碼解析—YYModel(YYClassInfo)分析了如何根據(jù)OC的Class對(duì)象構(gòu)建...
    egoCogito_panf閱讀 11,825評(píng)論 4 32
  • 無(wú)論我們的感受是什么,都能告訴我們很多關(guān)于自己的事情。感覺(jué)沒(méi)有對(duì)錯(cuò)之分,它們只是感受。感受是一種能量,而這種能量會(huì)...
    正面管教講師張素敏閱讀 374評(píng)論 0 0
  • 傻氣的人,喜歡給心,也許會(huì)被人騙,卻未必能得到別人的。 小六那天晚上打電話給我。電話那頭的她,很難形容,像是失去了...
    蚊子小姐_閱讀 458評(píng)論 0 2

友情鏈接更多精彩內(nèi)容