美文网首页
KVC探讨-自定义(四)

KVC探讨-自定义(四)

作者: GitArtOS | 来源:发表于2020-03-01 23:22 被阅读0次

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

  • Attributes: 简单属性,比如基本数据类型,字符串和布尔值,而诸如 NSNumber 和其它一些不可变类型比如 NSColor 也可以被认为是简单属性
  • ``To-one relationshis: 这些是具有自己属性的可变对象属性。即对象的属性可以更改,而无需更改对象本身。例如,一个Account对象可能具有一个 owner属性,该属性是GTPerson对象的实例,而 GTPerson 对象本身具有address属性。owner的地址可以更改,但却而无需更改Account 持有的owner属性。也就是说Accountowner 属性未被更改,只是address `被更改了。
  • To-many relationships: 这些是集合对象属性。尽管也可以使用自定义集合类,但是通常使用 NSArrayNSSet 的实例来持有此集合。
    上代码:
// 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的类别.

  • 既然针对的是对象,那么我们就应该是针对NSObject的一个扩展Category。
  • 之前我们了解了KVC有取值和设值的过程,所以我们要自定义setValue:forKey:以及ValueForKey:的方法。
1 setValue: forKey:方法
  • customSetValue:forKey:方法:
    • 查找set_set方法;
    • 根据accessInstanceVariablesDirectly 方法的返回值一次判断_key,_is<Key>,key, isKey等实例变量,找到实例变量并复制,结束。
    • 如果没找到抛异常。
2. valueForKey 方法
  • customValueForKey:方法:
    • 找到相关方法 get countOf objectInAtIndex找到返回相应方法的返回值作为结果;
    • 根据accessInstanceVariablesDirectly方法的返回值一次判断_key,_is<Key>,key,isKey等实例变量,找到实例变量直接返回,结束。
  • 如果没找到抛异常。

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探讨-具体应用以及属性验证、异常处理(三)

相关文章

网友评论

      本文标题:KVC探讨-自定义(四)

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