一:KVC是什么?
KVC是Key Value Coding的簡稱娱局,即鍵值編碼彰亥,提供一種機制來間接訪問對象的屬性。而不是通過調用Setter衰齐、Getter方法訪問任斋,KVC的方法定義在Foundation/NSKeyValueCoding中,是一個NSObject的擴展耻涛,任何繼承NSObject的類都包含此方法废酷。簡單的來說,KVC讓我們能夠使用屬性的字符串名稱來設置抹缕、讀取屬性的值澈蟆,而不是通過.語法實現。
二:KVC能夠干什么卓研?
- 對私有變量進行賦值
- 字典轉模型
三:KVC的四個常用方法
// 根據屬性名稱key設置值
- (void)setValue:(nullable id)value forKey:(NSString *)key;
// 根據屬性路徑名稱keyPath設置值
- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;
// 根據屬性名稱key獲取值
- (nullable id)valueForKey:(NSString *)key;
// 根據屬性路徑名稱keyPath獲取值
- (nullable id)valueForKeyPath:(NSString *)keyPath;
1:KVC是如何獲取值趴俘?之- (nullable id)valueForKey:(NSString *)key;
方法
其實這些方法在Foundation框架NSKeyValueCoding.h中都有對方法的解析
關于- (nullable id)valueForKey:(NSString *)key;
獲取屬性值的調用過程如下:
1: 首先查找與其名稱匹配的幾個方法,判斷調用順序為:
get<key>奏赘,<key>寥闪,is<key>
2:如果以上三個方法均沒有,則調用+(BOOL)accessInstanceVariablesDirectly;
(是否直接訪問成員變量)磨淌,返回YES則直接訪問成員變量疲憋,返回NO,則調用- (id)valueForUndefinedKey:(NSString *)key
指定一個值返回梁只,不重寫此方法默認崩潰
3:直接訪問成員變量缚柳,也會順序判斷調用:_<key>、_is<key>敛纲、<key>喂击、is<key>
,如果沒有找到這些成員變量淤翔,則調用- (id)valueForUndefinedKey:(NSString *)key
指定一個值返回翰绊,不重寫此方法默認崩潰
代碼測試1:匹配方法
在TestKVCModel類中實現如下方法
@implementation TestKVCModel
//1: 獲取kvc最高級
- (NSString *)getTestName {
NSLog(@"getTestName");
return @"xxx";
}
//2: 獲取第二級
- (NSString *)testName {
NSLog(@"testName");
return @"xxx";
}
//3: 獲取第三級
- (NSString *)isTestName {
NSLog(@"isTestName");
return @"xxx";
}
// 是否允許直接方法成員變量
+ (BOOL)accessInstanceVariablesDirectly {
NSLog(@"accessInstanceVariablesDirectly");
return NO;
}
// 沒有訪問到,默認返回值,不實現此方法监嗜,默認崩潰
- (id)valueForUndefinedKey:(NSString *)key {
NSLog(@"valueForUndefinedKey %@", key);
return @"AAAAA";
}
使用- (nullable id)valueForKey:(NSString *)key;
方法訪問testName的值
TestKVCModel *model = [[TestKVCModel alloc] init];
NSLog(@"%@", [model valueForKey:@"testName"]);
多次輸出結果:
1:
2019-08-21 10:04:58.378737+0800 TestKVC[47554:2700945] getTestName
2019-08-21 10:04:58.378872+0800 TestKVC[47554:2700945] xxx
2:注釋- (NSString *)getTestName方法
2019-08-21 10:06:02.375669+0800 TestKVC[47614:2702788] testName
2019-08-21 10:06:02.375849+0800 TestKVC[47614:2702788] xxx
3:注釋- (NSString *)getTestName和- (NSString *)testName方法
2019-08-21 10:06:38.428103+0800 TestKVC[47654:2703877] isTestName
2019-08-21 10:06:38.428253+0800 TestKVC[47654:2703877] xxx
4:同時注釋三個方法
2019-08-21 10:07:15.966436+0800 TestKVC[47693:2705005] accessInstanceVariablesDirectly
2019-08-21 10:07:15.966592+0800 TestKVC[47693:2705005] accessInstanceVariablesDirectly
2019-08-21 10:07:15.966689+0800 TestKVC[47693:2705005] valueForUndefinedKey testName
2019-08-21 10:07:15.966769+0800 TestKVC[47693:2705005] AAAAA
測試結果:我們分別注釋TestKVCModel中的方法谐檀,可以觀察其調用順序,其調用順序依次為- (NSString *)getTestName
-> - (NSString *)testName
-> - (NSString *)isTestName
注釋上面三個方法默認返回值AAAAA裁奇,同時注釋- (id)valueForUndefinedKey:(NSString *)key
方法桐猬,程序會崩潰
接下來,我們?yōu)門estKVCModel類添加testName實例變量刽肠,+ (BOOL)accessInstanceVariablesDirectly
返回YES允許直接訪問實列變量
代碼測試2:匹配實例變量
在TestKVCModel.h中添加三個屬性
// 使用@property方式申明溃肪,會默認生成_key的實列變量,并默認實現getter和setter方法
@interface TestKVCModel : NSObject {
NSString *_testName;
NSString *testName;
NSString *isTestName;
}
在TestKVCModel.m中音五,設置初始值惫撰,允許直接方法成員變量
- (instancetype)init
{
self = [super init];
if (self) {
_testName = @"_testName";
testName = @"testName";
isTestName = @"isTestName";
}
return self;
}
// 是否允許直接方法成員變量
+ (BOOL)accessInstanceVariablesDirectly {
NSLog(@"accessInstanceVariablesDirectly");
return YES;
}
// 沒有訪問到,默認返回值躺涝,不實現此方法厨钻,默認崩潰
- (id)valueForUndefinedKey:(NSString *)key {
NSLog(@"valueForUndefinedKey %@", key);
return @"AAAAA";
}
使用- (nullable id)valueForKey:(NSString *)key;
方法訪問testName的值
TestKVCModel *model = [[TestKVCModel alloc] init];
NSLog(@"%@", [model valueForKey:@"testName"]);
多次輸出結果:
1:
2019-08-21 10:17:54.136509+0800 TestKVC[48278:2719585] accessInstanceVariablesDirectly
2019-08-21 10:17:54.136658+0800 TestKVC[48278:2719585] _testName
2:注釋_testName
2019-08-21 10:18:26.850766+0800 TestKVC[48323:2720715] accessInstanceVariablesDirectly
2019-08-21 10:18:26.850964+0800 TestKVC[48323:2720715] testName
3:注釋_testName和testName
2019-08-21 10:18:56.617613+0800 TestKVC[48362:2721753] accessInstanceVariablesDirectly
2019-08-21 10:18:56.617764+0800 TestKVC[48362:2721753] isTestName
4:全部注釋
2019-08-21 10:19:24.119137+0800 TestKVC[48401:2722593] accessInstanceVariablesDirectly
2019-08-21 10:19:24.119269+0800 TestKVC[48401:2722593] accessInstanceVariablesDirectly
2019-08-21 10:19:24.119399+0800 TestKVC[48401:2722593] valueForUndefinedKey testName
2019-08-21 10:19:24.119510+0800 TestKVC[48401:2722593] AAAAA
測試結果:我們分別注釋TestKVCModel中的實列變量,可以觀察其調用順序坚嗜,其調用順序依次為_testName -> testName -> isTestName
注釋上面三個方法默認返回值AAAAA夯膀,同時注釋-(id)valueForUndefinedKey:(NSString *)key
方法,程序會崩潰
另外苍蔬,- (nullable id)valueForKey:(NSString *)key
也適用于類方法
代碼測試3:匹配類方法
在TestKVCModel類中添加類方法如下
//1: 獲取kvc最高級
+ (NSString *)getTestName {
NSLog(@"getTestName");
return @"xxx11";
}
//2: 獲取第二級
+ (NSString *)testName {
NSLog(@"testName");
return @"xxx11";
}
//3: 獲取第三級
+ (NSString *)isTestName {
NSLog(@"isTestName");
return @"xxx11";
}
//4:沒有訪問到诱建,默認返回值,不實現此方法银室,默認崩潰
+ (id)valueForUndefinedKey:(NSString *)key {
NSLog(@"valueForUndefinedKey %@", key);
return @"AAAAA11";
}
使用類方法調用- (nullable id)valueForKey:(NSString *)key;
訪問testName的值
NSLog(@"%@", [TestKVCModel valueForKey:@"testName"]);
輸出結果:
1:
2019-08-21 09:30:49.017583+0800 TestKVC[45743:2657198] getTestName
2019-08-21 09:30:49.017710+0800 TestKVC[45743:2657198] xxx11
2:注釋+ (NSString *)getTestName方法
2019-08-21 10:00:02.245279+0800 TestKVC[47231:2691526] testName
2019-08-21 10:00:02.245431+0800 TestKVC[47231:2691526] xxx11
3:注釋+ (NSString *)getTestName和+ (NSString *)testName方法
2019-08-21 10:00:43.002210+0800 TestKVC[47274:2692668] isTestName
2019-08-21 10:00:43.002343+0800 TestKVC[47274:2692668] xxx11
4:同時注釋三個方法
2019-08-21 10:03:06.344779+0800 TestKVC[47426:2696736] valueForUndefinedKey testName
2019-08-21 10:03:06.344918+0800 TestKVC[47426:2696736] AAAAA11
測試結果:與代碼測試1的實列方法調用一致涂佃,使用場景:在組件中獲取pch文件或者其他組件定義的常量或者宏,可以在項目中為組件的類創(chuàng)建分類蜈敢,使用- (nullable id)valueForKey:(NSString *)key;
獲取值
2:KVC是如何獲取值?之- (nullable id)valueForKeyPath:(NSString *)keyPath;
方法
- (nullable id)valueForKeyPath:(NSString *)keyPath;
方法是根據路徑來獲取值汽抚,可以獲取對象中對象的實例變量值:
NSLog(@"%@", [model valueForKeyPath:@"subModel.nickName"]);
其查找的方式與- (nullable id)valueForKey:(NSString *)key;
一致抓狭,先匹配方法,再匹配實列變量
代碼測試1:獲取對象中對象的值
新建一個SubTestKVCModel類造烁,內容代碼如下
SubTestKVCModel.m中
//1: 獲取kvc最高級
- (NSString *)getNickName {
NSLog(@"getNickName");
return @"111";
}
//2: 獲取第二級
- (NSString *)nickName {
NSLog(@"nickName");
return @"111";
}
//3: 獲取第三級
- (NSString *)isNickName {
NSLog(@"isNickName");
return @"111";
}
// 是否允許直接方法成員變量
+ (BOOL)accessInstanceVariablesDirectly {
NSLog(@"accessInstanceVariablesDirectly");
return NO;
}
// 沒有訪問到否过,默認返回值,不實現此方法惭蟋,默認崩潰
- (id)valueForUndefinedKey:(NSString *)key {
NSLog(@"valueForUndefinedKey %@", key);
return @"BBBBB";
}
TestKVCModel.h中
@property (nonatomic, strong) SubTestKVCModel *subModel;
使用- (nullable id)valueForKeyPath:(NSString *)keyPath;
獲取TestKVCModel中的subModel的nickName的值
TestKVCModel *model = [[TestKVCModel alloc] init];
model.subModel = [[SubTestKVCModel alloc] init];
NSLog(@"%@", [model valueForKeyPath:@"subModel.nickName"]);
輸出結果:
2019-08-21 13:14:07.802015+0800 TestKVC[57293:2933510] getNickName
2019-08-21 13:14:07.802168+0800 TestKVC[57293:2933510] 111
也可以多層索引
NSLog(@"%@", [model valueForKeyPath:@"subModel.subSubModel.myName"]);
3:關于- (nullable id)valueForKeyPath:(NSString *)keyPath
的其他使用
1:在數組中:取最小值苗桂、最大值、平均值告组、求和
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
2:在數組中:去除重復元素
NSArray *ary = @[@"a", @"b", @"c", @"d", @"a", @"e", @"c"];
NSArray *resultAry = [ary valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"%@", resultAry);
輸出:
(
c,
d,
e,
a,
b
)
更多實用方法見:http://www.reibang.com/p/ff17a9619894
4:KVC是如何設置值的煤伟?之- (void)setValue:(nullable id)value forKey:(NSString *)key;
方法
關于- (void)setValue:(nullable id)value forKey:(NSString *)key;
設置屬性值的調用過程如下:
1:查找與其名匹配的
setKey
方法
2:找到setKey
方法,判斷setKey
的參數類型,如果類型非指針類型便锨,且setValue設置的值為nil围辙,則調用- (void)setNilValueForKey:(NSString *)key
方法
3:如果沒有找到setKey
方法,則調用+ (BOOL)accessInstanceVariablesDirectly
(是否允許直接方法成員變量)放案,返回YES則直接訪問成員變量姚建,返回NO,則調用- (void)setValue:(id)value forUndefinedKey:(NSString *)ke
吱殉,不重寫此方法默認崩潰
4:直接訪問成員變量掸冤,查找成員變量順序:_<key>、_is<Key>友雳、 <key>贩虾、 is<Key>
測試代碼1
.h文件
@interface TestKVCModel : NSObject {
int _testName;
int _isTestName;
int testName;
int isTestName;
}
.m文件
//// kvc最高優(yōu)先級
//- (void)setTestName:(NSString *)testName {
// NSLog(@"setTestName %@", testName);
//}
// kvc最高優(yōu)先級
- (void)setTestName:(int)testName {
NSLog(@"setTestName int %d", testName);
}
// 實現setKey方法,判斷setKey的參數如果非指針類型沥阱,且設置的值為nil缎罢,則調用此方法
- (void)setNilValueForKey:(NSString *)key {
NSLog(@"setNilValueForKey %@", key);
}
// 沒有訪問到,調用此方法考杉,沒有重寫此方法會崩潰
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(@"setValue forUndefinedKey %@", key);
}
// 沒有實現setKey方法策精,是否允許直接方法成員變量
+ (BOOL)accessInstanceVariablesDirectly {
NSLog(@"accessInstanceVariablesDirectly");
return YES;
}
調用- (void)setValue:(nullable id)value forKey:(NSString *)key;
方法設置值
TestKVCModel *model = [[TestKVCModel alloc] init];
[model setValue:nil forKey:@"testName"];
依次注釋代碼,進行測試崇棠,測試結論如上
5:KVC是如何設置值的咽袜?之- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath
方法
- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;
方法根據路徑設置值,可以設置對象中對象的實例變量值:
[model setValue:nil forKeyPath:@"subModel.nickName"];
設置值的調用過程與- (void)setValue:(nullable id)value forKey:(NSString *)key;
方法一致
測試代碼1
TestKVCModel *model = [[TestKVCModel alloc] init];
model.subModel = [[SubTestKVCModel alloc] init];
[model setValue:nil forKeyPath:@"subModel.nickName"];