iOS开发KVC——what is about kvc and how to use it ? (总结更新版)

1.what  is  about   kvc ?

KVC key valued coding 键值编码

KVC通过键值间接编码

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

2.常用的:

valueForKey: 

 valueForKeyPath: 

 dictionaryWithValuesForKeys: 输入一组key,返回这组key的所有值;

 setValue:forKey:

 setValue:forKeyPath: 

 setValuesForKeysWithDictionary: 输入一个key-value的dictionary,调用setValue:forKey:进行一个一个的设置操作;

dictionaryWithValuesForKeys: 输入一组key,返回这组key的所有值;

 setValuesForKeysWithDictionary: 输入一个key-value的dictionary,调用setValue:forKey:进行一个一个的设置操作;


3.how  to use it ?Let us learn learnt it with  Code examples

//
//  Person.h
//  kVC


#import <Foundation/Foundation.h>

@class Dog;

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, strong) NSArray *books;

@property (nonatomic, strong) Dog *dog;

- (void)printHeight;
@end

//
//  Person.m
//  kVC


#import "Person.h"

@implementation Person
{
    @private
    double _height;
}

- (void)printHeight
{
    NSLog(@"height是%f", _height);
}
@end


//
//  Dog.h
//  kVC


#import <Foundation/Foundation.h>
@class Bone;

@interface Dog : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) Bone *bone;
@end

//
//  Bone.h
//  kVC


#import <Foundation/Foundation.h>

@interface Bone : NSObject
@property (nonatomic, strong) NSString *type;
@end

//
//  Book.h
//  kVC


#import <Foundation/Foundation.h>

@interface Book : NSObject
@property (nonatomic, assign) double price;
@property (nonatomic, copy) NSString *name;
@end


//
//  main.m
//  kVC


#import <Foundation/Foundation.h>
#import "Person.h"
#import "Dog.h"
#import "Bone.h"
#import "Book.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Person *p = [[Person alloc] init];
        
        Book *book1 = [[Book alloc] init];
        book1.name = @"5分钟突破iOS开发";
        book1.price = 10.5;
        
        Book *book2 = [[Book alloc] init];
        book2.name = @"5分钟突破android开发";
        book2.price = 18.5;
        
        Book *book3 = [[Book alloc] init];
        book3.name = @"5分钟突破前端开发";
        book3.price = 20.5;
        
        Book *book4 = [[Book alloc] init];
        book4.name = @"5分钟突破PHP开发";
        book4.price = 10.5;
        
        p.books = @[book1, book2, book3, book4];
        
        // 获得所有的书名(将所有的书名放到一个数组中)
//        NSMutableArray *names = [NSMutableArray array];
//        for (Book *book in p.books) {
//            [names addObject:book.name];
//        }
        
        // 取出books数组中每一个元素的name属性值,放到一个新的数组中返回
//        NSArray *names = [p valueForKeyPath:@"books.name"];
//        [p valueForKeyPath:@"dog.name"];
//        NSLog(@"%@", names);
        
//        NSNumber *avgNumber = [p valueForKeyPath:@"[email protected]"];
        NSNumber *sumNumber = [p valueForKeyPath:@"[email protected]"];
        NSLog(@"%@", sumNumber);
//        NSLog(@"%f", [sumNumber doubleValue]);
    }
    return 0;
}

/**
 *  取值
 */
void test3()
{
    Person *p = [[Person alloc] init];
    p.dog = [[Dog alloc] init];
    p.dog.bone = [[Bone alloc] init];
    
    //        Cocoa的大招
    
    NSLog(@"%@", [p valueForKeyPath:@"dog.bone"]);
}

/**
 *  key和keyPath的区别
 */
void test2()
{
    Person *p = [[Person alloc] init];
    p.dog = [[Dog alloc] init];
    p.dog.bone = [[Bone alloc] init];
    //        p.dog.bone.type = @"狗骨";
    
    //        [p setValue:@"猪骨" forKeyPath:@"dog.bone.type"];
    //        [p.dog setValue:@"猪骨" forKeyPath:@"bone.type"];
    [p.dog.bone setValue:@"猪骨" forKeyPath:@"type"];
    
    NSLog(@"%@", p.dog.bone.type);
    
    //        p.dog.name = @"wangwang";
    //        [p.dog setValue:@"wangcai" forKey:@"name"];
    //        [p.dog setValue:@"larry" forKeyPath:@"name"];
    
    //        [p setValue:@"hashiqi" forKeyPath:@"dog.name"];
    
    //forKeyPath包含了forKey的功能,以后使用forKeyPath就可以了
    //forKeyPath中可以利用.运算符, 就可以一层一层往下查找对象的属性
    //        [p setValue:@"hashiqi" forKey:@"dog.name"]; // 写法错误
    
    //        NSLog(@"%@", p.dog.name);
}

void test()
{
    Person *p = [[Person alloc] init];
    //        p.name = @"rose";
    //        p.age = 20;
    //        [p setValue:@"jack" forKey:@"name"];
    //        [p setValue:@30 forKey:@"age"];
    
    // 利用KVC可以随意修改一个对象的属性或者成员变量(并且私有的也可以修改)
    
    //        [p setValue:@"jack" forKeyPath:@"_name"];
    //        [p setValue:@30 forKeyPath:@"_age"];
    //        NSLog(@"%d %@", p.age, p.name);
    
    
    [p setValue:@10.5 forKey:@"height"];
    [p printHeight];
}

? 利用KVC可以随意修改一个对象的属性或者成员变量(并且私有的也可以修改)
   forKeyPath包含了forKey的功能,以后使用forKeyPath就可以了
   forKeyPath中可以利用.运算符, 就可以一层一层往下查找对象的属性


4.setValuesForKeysWithDictionary:

当项目开发中遇到字典转模型的时候往往用这个
实例:
http://blog.csdn.net/sunnyboy9/article/details/16118401
注意这个例子中的控制器和模型类

你可能感兴趣的:(iOS开发KVC——what is about kvc and how to use it ? (总结更新版))