簡(jiǎn)述:對(duì)私有變量進(jìn)行賦值肥缔。
- 在系統(tǒng)框架 Foundation 中 NSKeyValueCoding.h 有更多的一些方法谢揪。
- KVC具體應(yīng)用場(chǎng)景:有待細(xì)究(還望告知)
單個(gè)類KVC 賦值基礎(chǔ)數(shù)據(jù)孝情。
- 賦值:setValue: forKey:
- 獲缺⒁ā:valueForKey:
例如有一個(gè) Person 的類谢澈,包含一些私有屬性煌贴。
@interface Person ()
{
// 大致包含以下幾類數(shù)據(jù)
NSString *_name;
NSNumber *_age;
NSArray *_colors;
NSDictionary *_family;
NSMutableDictionary *_works;
NSOrderedSet *_aOrderSet;
NSSet *_aSet;
NSMutableArray *_aMuArray;
NSMutableSet *_aMuSet;
NSMutableOrderedSet *_aMuOrderSet;
}
@end
在其他調(diào)用時(shí)可以使用KVC,進(jìn)行賦值锥忿,以及獲取牛郑。類似的不重復(fù)舉例。
Person *aPerson = [[Person alloc] init];
// NSString
[aPerson setValue:@"村長(zhǎng)" forKey:@"_name"];
NSString *value_name = [aPerson valueForKey:@"name"];
NSLog(@"%@",value_name);
// NSNumber
[aPerson setValue:@12 forKey:@"_age"];
NSNumber *value_age = [aPerson valueForKey:@"_age"];
NSLog(@"%@",value_age);
// NSArray
[aPerson setValue:@[[UIColor redColor],[UIColor grayColor]] forKey:@"_colors"];
NSArray *value_colors = [aPerson valueForKey:@"_colors"];
self.view.backgroundColor = value_colors[0];
// NSMutableArray
NSMutableArray *muarr = [NSMutableArray array];
[muarr addObject:@"one"];
[muarr addObject:@"two"];
[aPerson setValue:muarr forKey:@"_aMuArray"];
NSMutableArray *value_muArr = [aPerson mutableArrayValueForKey:@"_aMuArray"];
NSLog(@"%@",value_muArr);
[value_muArr addObject:@"three"];
NSLog(@"%@",value_muArr);
系統(tǒng)還封裝了 KVC 開(kāi)關(guān) 以及 檢查
- 如果對(duì)某個(gè)類敬鬓,不允許使用KVC淹朋,可以通過(guò)設(shè)置 accessInstanceVariablesDirectly 控制灶似。
// 在該類的內(nèi)部,重寫(xiě)此方法瑞你,外部使用KVC時(shí)酪惭,禁用沒(méi)有寫(xiě)set get 方法的屬性值。
// 注意:對(duì)于 @property 定義的屬性可以 KVC
+ (BOOL)accessInstanceVariablesDirectly{
return NO;
}
- 賦值檢查
// 在類的內(nèi)部者甲,進(jìn)行檢查春感,不符合要求 返回NO ,提供外部參考虏缸。
- (BOOL)validateValue:(inout id _Nullable __autoreleasing *)ioValue forKey:(NSString *)inKey error:(out NSError * _Nullable __autoreleasing *)outError{
if ([inKey isEqualToString:@"colors"] && [*ioValue isKindOfClass:[NSArray class]]) {
return YES;
} else {
return NO;
}
}
// 外部 使用時(shí)鲫懒,先判斷是否符合要求,再使用KVC刽辙。
NSError *error;
NSString *apoint = @"name";
if ([aPerson validateValue:&apoint forKey:@"_colors" error:&error]) {
NSLog(@"可以賦值 apoint");
[aPerson setValue:apoint forKey:@"_colors"];
} else {
NSLog(@"不可以賦值 apoint");
NSLog(@"%@",error.debugDescription);
}
多重嵌套類 KVC
- 賦值:setValue: forKeyPath:
- 獲瓤摇:valueForKeyPath:
例如:有一個(gè)Person 類,擁有一個(gè) Cat 類宰缤。
@interface Person ()
@property (nonatomic, strong) Cat *mycat;
@end
Cat 類具有屬性 name
@interface Cat ()
@property (nonatomic, strong) NSString *name;
@end
外部使用時(shí)
Person *aPerson = [[Person alloc] init];
Cat *acat = [[Cat alloc] init];
// 鏈接關(guān)系 非常關(guān)鍵
[aPerson setValue:acat forKey:@"mycat"];
// 路徑賦值 取值
[aPerson setValue:@"我的貓咪" forKeyPath:@"mycat.name"];
NSString *value_mycat_name = [aPerson valueForKeyPath:@"mycat.name"];
NSLog(@"%@",value_mycat_name);
其他 還有重寫(xiě) 未定義的 key 颂翼,nil Value 的key 等 幾個(gè)方法。
- 簡(jiǎn)單了解重寫(xiě)
// 多 定義了一個(gè) key 用于存 一些外部傳入沒(méi)有的key
@property (nonatomic, strong) NSString *undefineKey;
// 重寫(xiě) 取 沒(méi)有定義key 時(shí) 返回
- (nullable id)valueForUndefinedKey:(NSString *)key{
return [self valueForKey:self.undefineKey];
}
// 重寫(xiě) 賦值沒(méi)有定義的key時(shí)慨灭,賦值
- (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key{
[self setValue:value forKeyPath:self.undefineKey];
}
// 重寫(xiě) 賦值為nil 時(shí) 朦乏,修改賦值
- (void)setNilValueForKey:(NSString *)key{
[self setValue:@"" forKeyPath:key];
}
其他
- 剩余一些方法 例如 :
setValuesForKeysWithDictionary 等。待開(kāi)拓
FOUNDATION_EXPORT NSString *const NSUndefinedKeyException;
FOUNDATION_EXPORT NSString *const NSAverageKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSCountKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSDistinctUnionOfArraysKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSDistinctUnionOfObjectsKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSDistinctUnionOfSetsKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSMaximumKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSMinimumKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSSumKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSUnionOfArraysKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSUnionOfObjectsKeyValueOperator;
FOUNDATION_EXPORT NSString *const NSUnionOfSetsKeyValueOperator;
@interface NSObject(NSKeyValueCoding)
- (NSMutableArray *)mutableArrayValueForKey:(NSString *)key;
- (NSMutableOrderedSet *)mutableOrderedSetValueForKey:(NSString *)key NS_AVAILABLE(10_7, 5_0);
- (NSMutableSet *)mutableSetValueForKey:(NSString *)key;
- (nullable id)valueForKeyPath:(NSString *)keyPath;
- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;
- (BOOL)validateValue:(inout id __nullable * __nonnull)ioValue forKeyPath:(NSString *)inKeyPath error:(out NSError **)outError;
- (NSMutableArray *)mutableArrayValueForKeyPath:(NSString *)keyPath;
- (NSMutableOrderedSet *)mutableOrderedSetValueForKeyPath:(NSString *)keyPath NS_AVAILABLE(10_7, 5_0);
- (NSMutableSet *)mutableSetValueForKeyPath:(NSString *)keyPath;
- (NSDictionary<NSString *, id> *)dictionaryWithValuesForKeys:(NSArray<NSString *> *)keys;
- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *, id> *)keyedValues;
@end
@interface NSArray<ObjectType>(NSKeyValueCoding)
- (id)valueForKey:(NSString *)key;
- (void)setValue:(nullable id)value forKey:(NSString *)key;
@end
@interface NSDictionary<KeyType, ObjectType>(NSKeyValueCoding)
- (nullable ObjectType)valueForKey:(NSString *)key;
@end
@interface NSMutableDictionary<KeyType, ObjectType>(NSKeyValueCoding)
- (void)setValue:(nullable ObjectType)value forKey:(NSString *)key;
@end
@interface NSOrderedSet<ObjectType>(NSKeyValueCoding)
- (id)valueForKey:(NSString *)key NS_AVAILABLE(10_7, 5_0);
- (void)setValue:(nullable id)value forKey:(NSString *)key NS_AVAILABLE(10_7, 5_0);
@end
@interface NSSet<ObjectType>(NSKeyValueCoding)
- (id)valueForKey:(NSString *)key;
- (void)setValue:(nullable id)value forKey:(NSString *)key;
@end