国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

KVC示例

2019-11-14 19:49:35
字體:
供稿:網(wǎng)友

KVC –key value Coding,可以讓我們通過鍵值編碼的形式進(jìn)行屬性值的賦值

參考蘋果官網(wǎng)的圖。。

1.KVC

定義一個Person類

.h文件

   1:  #import <Foundation/Foundation.h>
   2:  
   3:  @interface Person : NSObject
   4:  {
   5:     NSString *name;
   6:  }
   7:   
   8:  @end

.m文件

   1:  #import "Person.h"
   2:   
   3:  @implementation Person
   4:   
   5:  @end

 

通過普通的獲取屬性的方法是無法對name進(jìn)行賦值和取值操作的,這種情況可以通過KVC來進(jìn)行設(shè)置和取值

   1:  - (void)viewDidLoad {
   2:      [super viewDidLoad];
   3:      // Do any additional setup after loading the view, typically from a nib.
   4:     Person  person=[[Person alloc]init];
   5:      [person setValue:@"keith" forKey:@"name"];
   6:      NSLog(@"name is %@",[person valueForKey:@"name"]);
   7:  }
輸出:
2014-11-26 21:51:12.237 KVCtest[543:10453] name is keith

賦值用setValue:forKey:

取值用 valueForKey:

2.鍵路徑

如果類中包含其他的類怎么辦呢,訪問這個類的屬性怎么辦呢。可以使用setValue: forKeyPath: 和valueForKeyPath設(shè)置和獲取

如定義一個car

.h文件

#import <Foundation/Foundation.h>@interface Car : NSObject{    NSString *carname;}@end

將Person的.h改成

#import <Foundation/Foundation.h>@class Car;@interface Person : NSObject{    NSString *name;    Car *hiscar;}-(id)init;@end

.m為

#import "Person.h"#import "Car.h"@implementation Person-(id)init{    if (self=[super init]) {        hiscar=[[Car alloc]init];    }    return self;}@end

這里實現(xiàn)了car的實例化,如果不這么做在鍵值編程時候hiscar因為沒有初始化而出現(xiàn)無法賦值的情況

調(diào)用:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    Person *person=[[Person alloc]init];    [person setValue:@"keith" forKey:@"name"];    [person setValue:@"QQ" forKeyPath:@"hiscar.carname"];    NSLog(@"name is %@,hiscar is %@",[person valueForKey:@"name"],[person valueForKeyPath:@"hiscar.carname"]);        /*或者這樣*/     Car *car=[[Car alloc]init];     [car setValue:@"BeiKy" forKey:@"carname"];     [person setValue:car forKey:@"hiscar"];    NSLog(@"name is %@,hiscar is %@",[person valueForKey:@"name"],[person valueForKeyPath:@"hiscar.carname"]);}
輸出結(jié)果

2014-11-26 22:45:47.392 KVCtest[1031:25564] name is keith,hiscar is QQ
2014-11-26 22:45:47.394 KVCtest[1031:25564] name is keith,hiscar is BeiKy

3.類型封裝和轉(zhuǎn)換

 

再次修改Person.h文件

#import <Foundation/Foundation.h>@class Car;@interface Person : NSObject{    NSString *name;    Car *hiscar;    NSInteger age;//增加一個年齡類型是NSInteger}-(id)init;@end
 

 

那么如何在設(shè)置的時候會發(fā)生什么呢?

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    Person *person=[[Person alloc]init];    [person setValue:@"keith" forKey:@"name"];    [person setValue:@"QQ" forKeyPath:@"hiscar.carname"];    [person setValue:@"24" forKeyPath:@"age"];//為年齡賦值    NSLog(@"name is %@, is %@ old ,hiscar is %@",[person valueForKey:@"name"],[person valueForKey:@"age"],[person valueForKeyPath:@"hiscar.carname"]);}

結(jié)果是:

2014-11-26 22:56:08.873 KVCtest[1086:27956] name is keith, is 24 old ,hiscar is QQ

4.操作集合

 

在Person中添加一個otherPerson

#import <Foundation/Foundation.h>@class Car;@interface Person : NSObject{    NSString *name;    Car *hiscar;    NSInteger age;    NSMutableArray *otherPerson;}-(id)init;@end

屬性.@sum.屬性,還有@count,@avg,@max.@min

-(void)kvcCollection{    Person *person=[[Person alloc]init];    [person setValue:@"keith" forKey:@"name"];    Person *person1=[[Person alloc]init];    Person *person2=[[Person alloc]init];    Person *person3=[[Person alloc]init];    Person *person4=[[Person alloc]init];    NSMutableArray *others=[[NSMutableArray alloc]initWithObjects:person1,person2,person3,person4, nil];    NSInteger agenum=20;    for (Person *p in others) {        [self setage:p age:[[NSString alloc]initWithFormat:@"%ld",agenum++]];    }    [person setValue:others forKey:@"otherPerson"];    NSLog(@"other's age is %@",[person valueForKeyPath:@"otherPerson.age"]);    NSLog(@"the avg age is %@",[person valueForKeyPath:@"otherPerson.@avg.age"]);    NSLog(@"the sum age is %@",[person valueForKeyPath:@"otherPerson.@sum.age"]);    NSLog(@"the count person is %@",[person valueForKeyPath:@"otherPerson.@count.age"]);    NSLog(@"the min age is %@",[person valueForKeyPath:@"otherPerson.@min.age"]);    NSLog(@"the max age is %@",[person valueForKeyPath:@"otherPerson.@max.age"]);    }-(void)setage:(Person *)person age:(NSString *)age{    [person setValue: age forKey:@"age"];}

 

輸出

2014-11-26 23:51:00.180 KVCtest[1346:42300] other's age is (
    20,
    21,
    22,
    23
)
2014-11-26 23:51:00.183 KVCtest[1346:42300] the avg age is 21.5
2014-11-26 23:51:00.183 KVCtest[1346:42300] the sum age is 86
2014-11-26 23:51:00.184 KVCtest[1346:42300] the count person is 4
2014-11-26 23:51:00.184 KVCtest[1346:42300] the min age is 20
2014-11-26 23:51:00.184 KVCtest[1346:42300] the max age is 23

 

本文參考 容芳志博客,感謝這位博友

著作權(quán)聲明:本文由http://m.survivalescaperooms.com/keithmoring/原創(chuàng),歡迎轉(zhuǎn)載分享。請尊重作者勞動,轉(zhuǎn)載時保留該聲明和作者博客鏈接,謝謝!


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄山市| 沙湾县| 通江县| 新疆| 建始县| 阿拉善右旗| 弥勒县| 江达县| 怀安县| 江川县| 故城县| 定安县| 平原县| 永丰县| 体育| 马尔康县| 东安县| 昔阳县| 筠连县| 湛江市| 罗源县| 涟水县| 普格县| 盖州市| 东安县| 天柱县| 土默特左旗| 久治县| 交口县| 五莲县| 霞浦县| 临泉县| 丽水市| 莱阳市| 会昌县| 山丹县| 芦溪县| 安西县| 安国市| 义乌市| 桂林市|