IOS學(xué)習(xí)筆記七之KVC和Key路徑

1、KVC介紹

1)、KVC是由NSKeyValueCoding協(xié)議提供支持最基本的屬性和兩個(gè)方法如下

setValue:屬性值  forkey:屬性名:為指定屬性設(shè)置值

valueForKey:屬性名   (得到或者指定屬性的值)


2)、當(dāng)設(shè)置value為nil的時(shí)候,我們需要在類的實(shí)現(xiàn)里面重寫setNilValueForKey方法,不然會(huì)拋出NSInvalidArgumentException

 
2、key路徑介紹

KVC除了操作對象的屬性之外,還可以操作對象的“復(fù)合屬性”,比如類里面的成員變量是對象,然后給這個(gè)成員變量的對象進(jìn)行賦值,就這樣簡單理解,賦值之前一定要記得把這個(gè)對象進(jìn)行初始化操作

KVC協(xié)議中為操作Key路徑的方法如下


setValue:forKeyPath:根據(jù)Key設(shè)置屬性值

valueForKeyPath:根據(jù)key的路徑獲取屬性值

 
2、測試簡單Demo
 User.h

    #ifndef User_h
    #define User_h
    #import <Foundation/Foundation.h>
    @interface User : NSObject
    @property (nonatomic) NSString *name;
    @property (nonatomic, strong) NSString *city;
    @property (nonatomic, copy) NSString *add;
    @property NSString *pass;
    @property NSDate *birth;
    @property NSDate *birth1;
    @end
    #endif /* User_h */


User.m


    #import <Foundation/Foundation.h>
    #import "User.h"
     
    @implementation User
    @synthesize name = _name;
    @synthesize pass;
    @synthesize  birth;
    -(void) setName:(NSString *)name
    {
        self->_name = [NSString stringWithFormat:@"hello%@", name];
    }
    @end

 
KVCPerson.h

 
    #ifndef KVCPerson_h
    #define KVCPerson_h
    #import <Foundation/Foundation.h>
    #import "User.h"
     
    @interface KVCPerson : NSObject
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, copy) NSString *pass;
    @property (nonatomic, copy) NSDate *birth;
    @property (nonatomic) int price;
    @property (nonatomic) User *user;
    @end
    #endif /* KVCPerson_h */


KVCPerson.m

    #import <Foundation/Foundation.h>
    #import "KVCPerson.h"
    @implementation KVCPerson
     
    -(void)setNilValueForKey:(id)key
    {
        if ([key isEqualToString:@"price"])
        {
            _price = 0;
        }
        else
        {
            [super setNilValueForKey:key];
        }
    }
     
    @end


main.m

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    #import "Person.h"
    #import "Apple.h"
    #import "User.h"
    #import "Args.h"
    #import "KVCPerson.h"
    #import "FKItem.h"
    #import "FKOrder.h"
     
    int main(int argc, char * argv[]) {
        @autoreleasepool {
            KVCPerson *person = [KVCPerson new];
            [person setValue:@"chenyu" forKey:@"name"];
            [person setValue:@"1234" forKey:@"pass"];
            [person setValue:[NSDate date] forKey:@"birth"];
            NSLog(@"name is %@", [person valueForKey:@"name"]);
            NSLog(@"pass is %@", [person valueForKey:@"pass"]);
            NSLog(@"birth is %@", [person valueForKey:@"birth"]);
            NSLog(@"price is %@", [person valueForKey:@"price"]);
            [person setValue:nil forKey:@"name"];
            [person setValue:nil forKey:@"price"];
            NSLog(@"name is %@", [person valueForKey:@"name"]);
            NSLog(@"pass is %@", [person valueForKey:@"pass"]);
            NSLog(@"birth is %@", [person valueForKey:@"birth"]);
            NSLog(@"price is %@", [person valueForKey:@"price"]);
            
            [person setValue:[User new] forKeyPath:@"user"];
            //在使用valueForKeyPath之前一定要記得把成員對象進(jìn)行初始化,不然直接設(shè)置無效
            [person setValue:@"hello" forKeyPath:@"user.city"];
            NSLog(@"user.city is%@", [person valueForKeyPath:@"user.city"]);
            
        }
    }

 
3、運(yùn)行結(jié)果


    name is chenyu
    pass is 1234
    birth is Fri Jul  6 22:39:34 2018
    price is 0
    name is (null)
    pass is 1234
    birth is Fri Jul  6 22:39:34 2018
    price is 0
    user.city is hello


 
4、總結(jié)
在使用key的時(shí)候,一定要記得先把成員屬性的對象進(jìn)行初始化,不然設(shè)置無效

如下

            [person setValue:[User new] forKeyPath:@"user"];
            //在使用valueForKeyPath之前一定要記得把成員對象進(jìn)行初始化,不然直接設(shè)置無效
            [person setValue:@"hello" forKeyPath:@"user.city"];
            NSLog(@"user.city is%@", [person valueForKeyPath:@"user.city"]);

而不是

            [person setValue:@"hello" forKeyPath:@"user.city"];
            NSLog(@"user.city is%@", [person valueForKeyPath:@"user.city"]);

這樣結(jié)果會(huì)是null





 


作者:chen.yu
深信服三年半工作經(jīng)驗(yàn),目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號(hào):編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進(jìn)階人工智能(鏈接)