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 等。待开拓
相关文章推荐
- UI界面与实现功能的交互
- Building Maintainable Software-java篇之Keep Architecture Components Balanced
- hdu(1171)——Number Sequence
- Building Maintainable Software-java篇之Couple Architecture Components Loosely
- iOS 【UIKit-UIPageControl利用delegate定位圆点位置 之 四舍五入小技巧】
- UGUI实现NGUI的UIEventListener功能
- UGUI实现NGUI的UIEventListener功能
- Qt Quick的Canvas画图之arcTo
- 关于代码手写UI,xib和StoryBoard
- UI学习第07天
- HighGUI 图形用户界面
- CodeForces 625A Guest From the Past
- Arduino代码机制-WString.h
- 利用xcode查看UI层级关系
- 序列图(sequence)语法
- AssetBundleBuild 关于打包与加载
- Codeforces Round #342 (Div. 2)-A. Guest From the Past
- Codeforces Round #342 (Div. 2)-A. Guest From the Past
- MongoVUE破解
- 03-02-UITableView数据刷新