IOS的KVC

來源:互聯網
上載者:User

標籤:

示範代碼

  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 }

 

IOS的KVC

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.