UI基础之KVC介绍

一、KVC简单介绍

KVC key valued coding 键值编码

KVC通过键值间接编码

  补充:

     与KVC相对的时KVO,即key valued observer 键值观察。监听某一个模型的属性,只要模型属性的值一变化就通知你。

二、代码颜色

1> 通过KVC设置值  setValue:    forKey:

1,1>

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) Book *book;

@end

@implementation Person

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p1 = [[Person alloc] init];
        [p1 setValue:@"zhangsan" forKey:@"name"];
        p1.age = 18;
        
        NSLog(@"name = %@, age = %d", p1.name, p1.age);
    }
    return 0;
}

通过执行结果可以看到这个方法通过对象的属性值设置value。

1,2> 通过KVC设置值  setValue:    forKeyPath:

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) Book *book;

@end

@implementation Person

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p1 = [[Person alloc] init];
        [p1 setValue:@"zhangsan" forKeyPath:@"name"];
        p1.age = 18;
        
        NSLog(@"name = %@, age = %d", p1.name, p1.age);
    }
    return 0;
}

可以看到两个方法执行效果一样,

 [setValue:@"zhangsan" forKeyPath:@"name"];

要看到这个方法的好处,可以看下面这个例子:

1.3>

#import <Foundation/Foundation.h>

@interface Book : NSObject

@property (nonatomic, copy) NSString *bookName;

@end

@implementation Book

@end

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) Book *book;

@end

@implementation Person

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p1 = [[Person alloc] init];
        Book *b1 = [[Book alloc] init];
        
        p1.book = b1;
        [p1 setValue:@"同桌的你" forKeyPath:@"book.bookName"];
        
        NSLog(@"bookName = %@", b1.bookName);
    }
    return 0;
}

用KVC取一个嵌套层次很深的路径的时候,只要诶它一个路径就能把想要的属性给拿出来。(.可以理解为路径。一直一直进入)。能够帮助我们很方便的编码

2,通过KVC取值:

#import <Foundation/Foundation.h>

@interface Book : NSObject

@property (nonatomic, copy) NSString *bookName;

@end

@implementation Book

@end

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) Book *book;

@end

@implementation Person

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p1 = [[Person alloc] init];
        Book *b1 = [[Book alloc] init];
        b1.bookName = @"同桌的你";
        p1.book = b1;
        
        NSString *bookName = [p1 valueForKeyPath:@"book.bookName"];
        
        NSLog(@"bookName = %@", bookName);
    }
    return 0;
}

 结果:

当然我们也可以通过valueForKey:取值不过只能取person对象的属性的值,不能取到book对象中的属性值

3,将person对象放入到数组中,通过数组取的person对象中属性的值

代码:

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) Book *book;

@end

@implementation Person

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p1 = [[Person alloc] init];
        p1.name = @"zhangsan";
        
        Person *p2 = [[Person alloc] init];
        p2.name = @"lisi";
        
        NSArray *array = @[p1, p2];
        
        NSArray *name = [array valueForKeyPath:@"name"];

        NSLog(@"%@", name);


    }
    return 0;
}

结论:将对象放入到数组中,可以通过数组的valueForKeyPath:方法取出数组中存放对象的属性值,返回一个存放属性值的数组对象

4>KVC在字典中的应用

4.1 可以将字典中的键对应的值赋给对象的属性值

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) Book *book;

@end

@implementation Person

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p1 = [[Person alloc] init];
        
        NSDictionary *dic = @{@"name":@"zhangsan"};

        [p1 setValuesForKeysWithDictionary:dic];
        NSLog(@"%@", p1.name);


    }
    return 0;
}

4.2 可以将对象的属性值赋给字典键对应的值

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) Book *book;

@end

@implementation Person

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Person *p1 = [[Person alloc] init];
        p1.name = @"zhangsan";
        p1.age = 19;
       
      NSDictionary *dic = [p1 dictionaryWithValuesForKeys:@[@"name", @"age"]];
        NSLog(@"%@", dic);

    }
    return 0;
}

用KVC取一个嵌套层次很深的路径的时候,只要诶它一个路径就能把想要的属性给拿出来。(.可以理解为路径。一直一直进入)。能够帮助我们很方便的编码

原文地址:https://www.cnblogs.com/-boy/p/4116297.html