KVC(二)

接上: KVC(一)


接下来我们来看看苹果的官方文档

参考:Key-Value CodingKey-Value Coding(键值编码)

1.关于KCVC

  • 1.什么是Key-value coding(键值编码)

  • 通过由 NSKeyValueCoding 这种非正式的协议,可以实现对与遵循该协议的对象,对其属性可以间接的访问。

  • 符合键值的编码对象提供了一个简单的消息传递接口, 该接口与其所有属性都是一致的

  • 在Cocoa内使用场景:key-value observing(KVC)、 Cocoa bindingsCore DataAppleScript-ability等等

  • 2.作用

    • Access object properties(访问对象属性)
    • Manipulate collection properties(操作集合属性)
    • Invoke collection operators on collection objects(在集合对象上调用集合运算符)
    • Access non-object properties(访问非对象属性)
    • Access properties by key path(按键路径访问属性)
  • 3.对对象采用键值编码:遵循NSKeyValueCoding协议+实现相应的方法


2.下面我们来看看怎么使用

  • KVC简单使用
@interface CardModel:NSObject
@property (nonatomic,strong)NSString  *cardNumber;
@end
@implementation CardModel
@end

@interface MyPerson:NSObject
@property (nonatomic,strong)NSString  *name;
@end
@implementation MyPerson
@end

@interface BankAccount:NSObject
@property (nonatomic,strong)NSNumber  *currentBalance;
@property (nonatomic,strong)MyPerson  *owner;
@property (nonatomic,strong)NSArray<CardModel* >* cardModels;
@end

@implementation BankAccount
@end
//====================================END=============================================//

- (void)viewDidLoad {
    [super viewDidLoad];
    //我们分析一个BankAccount实例对象
    //里面有直接属性(Attributes)
    //一对一的 包含另外一个对象的属性(To-one relationships)
    //一对多的 包含另外一个对象的集合的属性(To-many relationships)(“集合”)
    
    CardModel *card1 = [[CardModel alloc]init];
    card1.cardNumber = @"123";
    CardModel *card2 = [[CardModel alloc]init];
    card2.cardNumber = @"123";
    
    MyPerson *p = [[MyPerson alloc]init];
    p.name = @"Jack";
    
    BankAccount *bankAccount = [[BankAccount alloc]init];
    bankAccount.currentBalance = @(0);
    bankAccount.owner = p;
    bankAccount.cardModels = @[card1,card2];
    
    
    //KVC的使用
    [bankAccount setValue:@(100.0) forKey:@"currentBalance"]; //[bankAccount setCurrentBalance:@(100.0)];
    NSLog(@"%@",bankAccount.currentBalance);
    NSLog(@"%@",[bankAccount valueForKey:@"currentBalance"]);
    
    [bankAccount setValue:@"Lucy" forKeyPath:@"owner.name"];
    NSLog(@"%@",bankAccount.owner.name);
    NSLog(@"%@",[bankAccount valueForKeyPath:@"owner.name"]);
    
    MyPerson *p2 = [[MyPerson alloc]init];
    p2.name = @"Kitty";
    NSDictionary *dict =@{
                          @"owner":p2,
                          @"currentBalance":@(1000.0)
                          };
    //该方法只能用于一级(比如owner.name不行的哟)
    [bankAccount setValuesForKeysWithDictionary:dict];
    NSLog(@"%@",bankAccount.currentBalance);
    NSLog(@"%@",bankAccount.owner.name);
    NSLog(@"%@",[bankAccount dictionaryWithValuesForKeys:@[@"owner",@"currentBalance"]]);
   
}

3.KVC的具体的设值原理(步骤) (具体的代码详见gitHub)

由于我们无法看到里面的源码,我们只能看,官方文档Accessor Search Patterns来猜测它的执行原理

以下代码:未分析集合类(对于集合类下面有具体的写)

#import "ViewController6.h"
@interface MyStudentSuperClass:NSObject
@property (nonatomic,strong) NSString *name1;
@end
@implementation MyStudentSuperClass{
@public
    NSString *_name;
    NSString *_isName;
    NSString *name;
    NSString *isName;
}
@end

@interface MyStudentSubClass:MyStudentSuperClass{
//    @public
//    NSString *_name;
//    NSString *_isName;
//    NSString *name;
//    NSString *isName;
}
@end
@implementation MyStudentSubClass
//是否可以直接访问成员变量
+(BOOL)accessInstanceVariablesDirectly{
    return YES;
}

//-(void)setName:(NSString *)name{
//    NSLog(@"%s",__FUNCTION__);
//}

//-(void)_setName:(NSString*)_name{
//    NSLog(@"%s",__FUNCTION__);
//}

//-(void)setIsName:(NSString*)isName{
//    NSLog(@"%s",__FUNCTION__);
//}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"没找到key:%@哟",key);
}
@end
//==============================================

@interface ViewController6 ()
@end

@implementation ViewController6
- (void)viewDidLoad {
    [super viewDidLoad];
    MyStudentSubClass *superC = [[MyStudentSubClass alloc]init];

    //KVC设置值
    [superC setValue:@"设置name值" forKey:@"name"];
    
    //setter方法会按照setKey、_setKey、setIsKey的优先级进行调用,还没有
    //(是否可以访问成员属性),
    //1.可以  则按_key、_isKey、key、isKey查找成员变量 ,找不到 forUndefinedKey
    //2.不可以 则:forUndefinedKey
    NSLog(@"_name%@",superC->_name);
    NSLog(@"_isName%@",superC->_isName);
    NSLog(@"name%@",superC->name);
    NSLog(@"isName%@",superC->isName);
}
@end

分析:根据上代码打开的顺序,以及不同的组合,我们可以发现


image

4.KVC的具体的取值原理(步骤)(具体的代码详见gitHub)

#import "ViewController7.h"
@interface ClassOne:NSObject{
@public
    NSString *_name;
    NSString *_isName;
    NSString *name;
    NSString *isName;
}
@end
@implementation ClassOne
@end

@interface ClassTwo:ClassOne
//@property (nonatomic,strong)NSString  *name;
@end
@implementation ClassTwo

//-(NSString*)getName{
//    return @"getName";
//}
//
//- (NSString *)name{
//    return @"name";
//}

//-(NSString*)isName{
//    return @"isName";
//}

-(id)valueForUndefinedKey:(NSString *)key{
    return @"UndefinedKey";
}
+(BOOL)accessInstanceVariablesDirectly{
    return YES;
}
@end
//==================

@interface ViewController7 ()
@end

@implementation ViewController7
- (void)viewDidLoad {
    [super viewDidLoad];
    ClassTwo *two = [[ClassTwo alloc]init];
    [two setValue:@"jack" forKey:@"name"];
    
    //KVC取值验证
    NSLog(@"%@",[two valueForKey:@"name"]);
    
    NSLog(@"======================");
    NSLog(@"_name%@",two->_name);
    NSLog(@"_isName%@",two->_isName);
    NSLog(@"name%@",two->name);
    NSLog(@"isName%@",two->isName);
}
@end

分析:根据上代码打开的顺序,以及不同的组合,我们可以发现


image

友情链接:

推荐阅读更多精彩内容