IOS的KVC

演示代码

  1 #import <Foundation/Foundation.h>
  2 #import "Person.h"
  3 int main(int argc, const char * argv[]) {
  4     @autoreleasepool {
  5        
  6 //        // 直接为对象的属性赋值
  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的方式为对象赋值
 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 = @"[email protected]";
 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:@"[email protected]" forKeyPath:@"email"];
 54 //         [p1 setValue:@18 forKeyPath:@"age"];
 55 //        [p1 setValue:d forKeyPath:@"dog"];
 56 //        
 57 //        // @"dog.name" 这个就叫做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" : @"[email protected]",
 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         //------------把对象转成字典---------------------------------------
 91         Person *p1 = [[Person alloc] init];
 92         p1.name = @"张三";
 93         p1.age = 15;
 94         p1.email = @"[email protected]";
 95         
 96         Dog *chihuahua = [[Dog alloc] init];
 97         chihuahua.name = @"吉娃娃";
 98         p1.dog = chihuahua;
 99         
100         // 把对象转成字典
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 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。