IOS的KVC
KVC作用
KVC類似于java中的反射,它是通過一個(gè)字符串 key 來獲取和設(shè)置對(duì)應(yīng)類中成員屬性的值
而key就是用來遍歷某一個(gè)類,去查找類內(nèi)部是否有與key同名的成員屬性
所以對(duì)于KVC來說,成員屬性無私有 共有之分,只要在類中,只要能找到相對(duì)應(yīng)的就可以設(shè)置值
作用:
1. 給私有的成員屬性賦值
2. 系統(tǒng)底層的給成員屬性賦值都是采用KVC
演示代碼
1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 6 // // 直接為對(duì)象的屬性賦值 7 // Person *p1 = [[Person alloc] init]; 8 // p1.name = @"張三"; 9 // 10 // Dog *chihuahua = [[Dog alloc] init]; 11 // chihuahua.name = @"吉娃娃"; 12 // p1.dog = chihuahua; 13 // 14 // //NSLog(@"%@ --- %@", p1.name, p1.dog.name); 15 // 16 // 17 // 18 // 19 // // 通過kvc的方式為對(duì)象賦值 20 // Dog *husky = [[Dog alloc] init]; 21 // husky.name = @"哈士奇"; 22 // 23 // 24 // [p1 setValue:@"李四" forKeyPath:@"name"]; 25 // [p1 setValue:@10 forKeyPath:@"age"]; 26 // [p1 setValue:husky forKeyPath:@"dog"]; 27 // 28 // 29 // NSLog(@"%@---%d", p1.name, p1.age); 30 // NSLog(@"%@", p1.dog.name); 31 32 33 // //----------------------------------- 34 // Person *p1 = [[Person alloc] init]; 35 // 36 // NSString *value = @"husky@yahoo.com"; 37 // 38 // NSString *property = @"email"; 39 // 40 // 41 // [p1 setValue:value forKeyPath:property]; 42 // 43 // NSLog(@"%@", p1.name); 44 // 45 // NSLog(@"%@", p1.email); 46 47 48 //------------------------------ 49 // Person *p1 = [[Person alloc] init]; 50 // Dog *d = [[Dog alloc] init]; 51 // 52 // [p1 setValue:@"rzc" forKeyPath:@"name"]; 53 // [p1 setValue:@"rzc@yahoo.com" forKeyPath:@"email"]; 54 // [p1 setValue:@18 forKeyPath:@"age"]; 55 // [p1 setValue:d forKeyPath:@"dog"]; 56 // 57 // // @"dog.name" 這個(gè)就叫做keyPath 或者叫 "屬性的路徑" 58 // [p1 setValue:@"哈士貓" forKeyPath:@"dog.name"]; 59 // NSLog(@"%@---%d---%@--%@",p1.name,p1.age, p1.email, p1.dog.name); 60 61 62 // NSDictionary *bz = @{ 63 // @"name" : @"任智超", 64 // @"age" : @28, 65 // @"email" : @"rzc0714@163.com", 66 // @"dog" : @{@"name" : @"加肥貓"} 67 // }; 68 // 69 // [p1 setValuesForKeysWithDictionary:bz]; 70 // NSDictionary *dogDict = (NSDictionary *)p1.dog; 71 // NSLog(@"%@---%d---%@--%@",p1.name,p1.age, p1.email, dogDict[@"name"]); 72 73 74 75 //--------------------------------------------------- 76 // Person *p1 = [[Person alloc] init]; 77 // p1.name = @"張三"; 78 // 79 // Dog *chihuahua = [[Dog alloc] init]; 80 // chihuahua.name = @"吉娃娃"; 81 // p1.dog = chihuahua; 82 // 83 // NSString *name = [p1 valueForKeyPath:@"name"]; 84 // NSString *dogName = [p1 valueForKeyPath:@"dog.name"]; 85 // 86 // NSLog(@"%@----%@", name, dogName); 87 88 89 90 //------------把對(duì)象轉(zhuǎn)成字典--------------------------------------- 91 Person *p1 = [[Person alloc] init]; 92 p1.name = @"張三"; 93 p1.age = 15; 94 p1.email = @"zs@yahoo.com"; 95 96 Dog *chihuahua = [[Dog alloc] init]; 97 chihuahua.name = @"吉娃娃"; 98 p1.dog = chihuahua; 99 100 // 把對(duì)象轉(zhuǎn)成字典 101 NSDictionary *dict = [p1 dictionaryWithValuesForKeys:@[@"name", @"age", @"email", @"dog"]]; 102 103 NSLog(@"%@", dict); 104 105 NSLog(@"%@", [dict[@"dog"] class]); 106 NSLog(@"%@", [dict[@"dog"] name]); 107 108 109 110 } 111 return 0; 112 }

浙公網(wǎng)安備 33010602011771號(hào)