zoukankan      html  css  js  c++  java
  • IOS的KVC

    KVC作用

    KVC类似于java中的反射,它是通过一个字符串 key 来获取和设置对应类中成员属性的值
    而key就是用来遍历某一个类,去查找类内部是否有与key同名的成员属性

    所以对于KVC来说,成员属性无私有 共有之分,只要在类中,只要能找到相对应的就可以设置值

    作用:
    1. 给私有的成员属性赋值
    2. 系统底层的给成员属性赋值都是采用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 = @"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" 这个就叫做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         //------------把对象转成字典---------------------------------------
     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         // 把对象转成字典
    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 }
  • 相关阅读:
    Beans
    HDU 1175 连连看
    HDU 1241 Oil Deposits dfs && bfs
    HDU1312:Red and Black
    背包问题
    canvas 和 svg
    前端性能优化---DOM操作
    四种常见的 POST 提交数据方式
    [转]浏览器缓存详解: expires, cache-control, last-modified, etag详细说明
    URL和URI
  • 原文地址:https://www.cnblogs.com/developer-wang/p/4534572.html
Copyright © 2011-2022 走看看