KVC探讨-自定义(四)

2020-03-01  本文已影响0人  GitArtOS

自定义之前先了解下对于属性的分类:

// GTPerson.h
@interface GTPerson
@property (nonatomic, copy) NSString *name; // Attributes 
@property (nonatomic, strong) Account *account; // To-one relationships
@property (nonatomic, strong) NSArray *subjects; // To-many relationships
@end

// Account.h
@interface Account
@property (nonatomic, assign) NSInteger balance; 
@end

我们实现聚焦于最常用的valueForKey:方法的声明,我们发现该方法是位于 NSKeyValueCoding这个分类里面的,这种设计模式可以实现解耦的功能
那么我们该怎么搞呢,这就来了

1.思路

系统的KVC是用NSObject的类别实现的,我们要自定义KVC无非也是系统的思路,实现个自定义KVC的类别.

1 setValue: forKey:方法
2. valueForKey 方法

2. 具体实现

- (void)customSetValue:(nullable id)value forKey:(NSString *)key{
    // 容错判断
    if (key == nil  || key.length == 0) return;
    // 找到相关方法 set<Key> _set<Key> setIs<Key>
    NSString *Key = key.capitalizedString;
    
    // 拼接方法
    NSString *setKey = [NSString stringWithFormat:@"set%@:",Key];
    NSString *_setKey = [NSString stringWithFormat:@"_set%@:",Key];
    NSString *setIsKey = [NSString stringWithFormat:@"setIs%@:",Key];
    NSArray *methodList = @[setKey,_setKey,setIsKey];
    for (NSInteger i = 0; i < methodList.count; i ++) {
        NSString *methodName = methodList[i];
        if ([self respondsToSelector:NSSelectorFromString(key)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [self performSelector:NSSelectorFromString(methodName) withObject:value];
#pragma clang diagnostic pop
            return;
        }
    }
    
    // 判断是否能够直接赋值实例变量
    if (![self.class accessInstanceVariablesDirectly] ) {
        @throw [NSException exceptionWithName:@"UnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.****",self] userInfo:nil];
    }
    
    // 找相关实例变量进行赋值
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i<count; i++) {
        Ivar ivar = ivars[i];
        const char *ivarNameChar = ivar_getName(ivar);
        NSString *ivarName = [NSString stringWithUTF8String:ivarNameChar];
        [mArray addObject:ivarName];
    }
    free(ivars);
    
    NSArray *appendPrefix = @[@"_",@"_is",@"",@"is"];
    for (NSInteger i = 0; i < appendPrefix.count; i ++) {
        NSString *instanceName = [NSString stringWithFormat:@"%@%@",appendPrefix[i],key];
        if ([mArray containsObject:instanceName]) {
            // 获取相应的 ivar
            Ivar ivar = class_getInstanceVariable([self class], instanceName.UTF8String);
            // 对相应的 ivar 设置值
            object_setIvar(self , ivar, value);
            return;
        }
    }
    
    // 如果找不到相关实例
    @throw [NSException exceptionWithName:@"UnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ %@]: this class is not key value coding-compliant for the key name.****",self,NSStringFromSelector(_cmd)] userInfo:nil];
}


- (id)customValueForKey:(NSString *)key{
    
    // 容错
    if (key == nil  || key.length == 0) {
        return nil;
    }
    
    // 找到相关方法 get<Key> <key> countOf<Key>  objectIn<Key>AtIndex
    NSString *Key = key.capitalizedString;
    // 拼接方法
    NSString *getKey = [NSString stringWithFormat:@"get%@",Key];
    NSString *countOfKey = [NSString stringWithFormat:@"countOf%@",Key];
    NSString *objectInKeyAtIndex = [NSString stringWithFormat:@"objectIn%@AtIndex:",Key];
        
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    if ([self respondsToSelector:NSSelectorFromString(getKey)]) {
        return [self performSelector:NSSelectorFromString(getKey)];
    }else if ([self respondsToSelector:NSSelectorFromString(key)]){
        return [self performSelector:NSSelectorFromString(key)];
    }else if ([self respondsToSelector:NSSelectorFromString(countOfKey)]){
        if ([self respondsToSelector:NSSelectorFromString(objectInKeyAtIndex)]) {
            int num = (int)[self performSelector:NSSelectorFromString(countOfKey)];
            NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
            for (int i = 0; i<num-1; i++) {
                num = (int)[self performSelector:NSSelectorFromString(countOfKey)];
            }
            for (int j = 0; j<num; j++) {
                id objc = [self performSelector:NSSelectorFromString(objectInKeyAtIndex) withObject:@(num)];
                [mArray addObject:objc];
            }
            return mArray;
        }
    }
#pragma clang diagnostic pop
    
    // 判断是否能够直接赋值实例变量
    if (![self.class accessInstanceVariablesDirectly] ) {
        @throw [NSException exceptionWithName:@"UnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.****",self] userInfo:nil];
    }
    
    // 找相关实例变量进行赋值
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i<count; i++) {
        Ivar ivar = ivars[i];
        const char *ivarNameChar = ivar_getName(ivar);
        NSString *ivarName = [NSString stringWithUTF8String:ivarNameChar];
        [mArray addObject:ivarName];
    }
    free(ivars);
 
    NSArray *appendPrefix = @[@"_",@"_is",@"",@"is"];
    for (NSInteger i = 0; i < appendPrefix.count; i ++) {
        NSString *instanceName = [NSString stringWithFormat:@"%@%@",appendPrefix[i],key];
        if ([mArray containsObject:instanceName]) {
            Ivar ivar = class_getInstanceVariable([self class], instanceName.UTF8String);
            return object_getIvar(self, ivar);;
        }
    }
    return nil;
}

KVC探讨-设定值 setValue: forKey:和取值valueForKey:(一)
KVC探讨-操作数组和集合、字典探讨(二)
KVC探讨-具体应用以及属性验证、异常处理(三)

上一篇 下一篇

猜你喜欢

热点阅读