iOS KVC 概述
KVCKVC 的基本概念 —–>WhatKVC 是一種間接更改對象狀態(或者說是屬性值)的方式:key-value coding 簡稱 KVC. 主要本質特點是採用字串來標識對象的屬性變數,並可以利用這個標識來更改對象的狀態(或者說是屬性值) 這種間接表現在通過字串來識別屬性,?而不是通過調?用存取?方法或直接地訪問執行個體變數的方式。 KVC機制不僅?支援對象,還?支援標量和結構體類型,這些?非對象的類型會被?自動的裝箱和開箱。Key & Key Path
鍵(Key)是?一個字串?用來標識對象?裡?面的?一個指定的屬性。?一般?一個鍵對應對象的存取?方法或 執行個體變數。鍵必須是ASCII碼,?一般以?小寫字?母開始,不能包含空格。
A key path is a string of dot separated keys that is used to specify a sequence of object properties to traverse. The property of the first key in the sequence is relative to the receiver, and each subsequent key is evaluated relative to the value of the previous property.
鍵路徑(Key Path)是?一個由點進?行分割的?一系列鍵組成的字串
鍵路徑的概念和表示:可以在對象和不同的變數名稱之間用圓點分開來表示.
注意:
鍵路徑的深度是任意的,具體取決於對象之間的關係的複雜度
KVC 基本用法
- (void)setValue:(id)value forKey:(NSString *)key - (id)valueForKey:(NSString *)key - (id)valueForKey:(NSString *)key //以 key 作為標示符,擷取其對應的屬性值 - (void)setValue:(id)value forKey:(NSString *)key //以 key 作為標示符設定其對應的屬性值 - (id)valueForUndefinedKey:(NSString *)key - (void)setNilValueForKey:(NSString *)key
KVC 的調用機制:valueForKey: 會首先尋找以參數名命名(格式為 -key 或者 isKey) 的 getter 方法,如果找到的話澤調用這個方法; 如果沒有找到這樣的getter 方法,它將會在對象內部尋找名稱格式為_ key 或者 key 的執行個體變數,然後返回. setValue:forKey: 的機制跟 valueForKey 相似.它會首先尋找參數名的 setter 方法,如果找到的話則完成設定; 如果沒有找到 setter 方法,則直接在類中找到名稱格式為_ key 或者 key 的執行個體變數,然後將 value 賦值給它.KVC 在集合中的使用
@avg
NSnumber *transactionAverage = [transactions valueForKeyPath:@@avg.amount];
@count
NSNumber *numberOfTransactions = [transactions valueForKeyPath:@@count]?;
@max
NSDate *latestDate = [transactions valueForKeyPath:@@max.date];
@min
NSDate *earliestDate = [transactions valueForKeyPath:@@min.date];
@sum
NSNumber *amountSum = [transactions valueForKeyPath:@@sum.amount];
使用索引值和鍵路徑的方法比較簡單,我就直接用一個 KVC在集合中的使用來示範
建立一個 QYPerson 類繼承於 NSObject
#import @class QYCourse;@interface QYPerson : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, strong) QYCourse *course;@property (nonatomic, assign) NSInteger point;@property (nonatomic, strong) NSArray *persons;@end
在 main.m 函數中
int main(int argc, const char * argv[]) { @autoreleasepool { QYPerson *person = [[QYPerson alloc] init]; QYPerson *p1 = [[QYPerson alloc] init]; QYPerson *p2 = [[QYPerson alloc] init]; QYPerson *p3 = [[QYPerson alloc] init]; [p1 setValue:@78 forKey:@point]; [p2 setValue:@87 forKey:@point]; [p3 setValue:@98 forKey:@point]; NSArray *array = @[p1,p2,p3]; [person setValue:array forKey:@persons]; NSLog(@other persons' achievement info:%@,[person valueForKeyPath:@persons.point]); NSLog(@all persons' number is %@,[person valueForKeyPath:@persons.@count]); NSLog(@the max achievement :%@,[person valueForKeyPath:@persons.@max.point]); NSLog(@the min achievement :%@,[person valueForKeyPath:@persons.@min.point]); NSLog(@the avg achievement :%@,[person valueForKeyPath:@persons.@avg.point]); } return 0;}
輸出的結果如下:
2015-07-29 15:25:48.856 KVCDemo[2968:1026943] other persons' achievement info:( 78, 87, 98)2015-07-29 15:25:48.856 KVCDemo[2968:1026943] all persons' number is 32015-07-29 15:25:48.856 KVCDemo[2968:1026943] the max achievement :982015-07-29 15:25:48.856 KVCDemo[2968:1026943] the min achievement :782015-07-29 15:25:48.876 KVCDemo[2968:1026943] the avg achievement :87.666666666666666666666666666666666666