您的位置:首页 > 产品设计 > UI/UE

Key-Value Coding (KVC)

2016-02-16 00:13 501 查看

Key-Value Coding (KVC)

简单来说:就是对私有变量进行赋值。

在系统框架 Foundation 中 NSKeyValueCoding.h 有更多的一些方法。

KVC具体应用场景:有待细究(还望告知)

单个类KVC 赋值基础数据。

赋值:setValue: forKey:

获取:valueForKey:

例如有一个 Person 的类,包含一些私有属性。

@interface Person ()
{
// 大致包含以下几类数据
NSString *_name;
NSNumber *_age;

NSArray *_colors;
NSDictionary *_family;
NSMutableDictionary *_works;

NSOrderedSet *_aOrderSet;
NSSet *_aSet;

NSMutableArray *_aMuArray;
NSMutableSet *_aMuSet;
NSMutableOrderedSet *_aMuOrderSet;
}
@end


在其他调用时可以使用KVC,进行赋值,以及获取。类似的不重复举例。

Person *aPerson = [[Person alloc] init];

// NSString
[aPerson setValue:@"村长" 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);


系统还封装了 KVC 开关 以及 检查

如果对某个类,不允许使用KVC,可以通过设置 accessInstanceVariablesDirectly 控制。

// 在该类的内部,重写此方法,外部使用KVC时,禁用没有写set get 方法的属性值。
// 注意:对于 @property 定义的属性可以 KVC
+ (BOOL)accessInstanceVariablesDirectly{
return NO;
}


赋值检查

// 在类的内部,进行检查,不符合要求 返回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;
}
}


// 外部 使用时,先判断是否符合要求,再使用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:

例如:有一个Person 类,拥有一个 Cat 类。

@interface Person ()
@property (nonatomic, strong) Cat *mycat;
@end


Cat 类具有属性 name

@interface Cat ()
@property (nonatomic, strong) NSString *name;
@end


外部使用时

Person *aPerson = [[Person alloc] init];
Cat *acat = [[Cat alloc] init];

// 链接关系 非常关键
[aPerson setValue:acat forKey:@"mycat"];

// 路径赋值 取值
[aPerson setValue:@"我的猫咪" forKeyPath:@"mycat.name"];
NSString *value_mycat_name = [aPerson valueForKeyPath:@"mycat.name"];
NSLog(@"%@",value_mycat_name);


其他 还有重写 未定义的 key ,nil Value 的key 等 几个方法。

简单了解重写

// 多 定义了一个 key 用于存 一些外部传入没有的key
@property (nonatomic, strong) NSString *undefineKey;

// 重写 取 没有定义key 时 返回
- (nullable id)valueForUndefinedKey:(NSString *)key{
return [self valueForKey:self.undefineKey];
}

// 重写 赋值没有定义的key时,赋值
- (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key{
[self setValue:value forKeyPath:self.undefineKey];
}

// 重写 赋值为nil 时 ,修改赋值
- (void)setNilValueForKey:(NSString *)key{
[self setValue:@"" forKeyPath:key];
}


剩余一些方法 例如 :

setValuesForKeysWithDictionary 等。待开拓
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: