接上 :KVC(三 )
接下来我们来看看 set/get异常处理、键值验证、非对象类型的处理
参考官网:Handling Non-Object Values、Adding Validation、Validating Properties、Representing Non-Object Values
参考其他:Key-Value Coding、Key-Value Coding(键值编码)
下面只不过是根据我自己的理解写的小测试,若想了解更多,可以查看上述参考
1.set/get异常处理
#import "ViewController12.h"
@interface Person_12 : NSObject
@end
@implementation Person_12
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
if([key isEqualToString:@"name"]){
NSLog(@"setValue-forUndefinedKey--key:%@",key);
}
}
-(id)valueForUndefinedKey:(NSString *)key{
if([key isEqualToString:@"name"]){
NSLog(@"valueForUndefinedKey--key:%@",key);
return nil;
}
return @"1";
}
@end
@interface Student_12 : NSObject
@property (nonatomic,strong)Person_12 *p;
@property (nonatomic,assign)NSInteger age;
@end
@implementation Student_12
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
if([key isEqualToString:@"name"]){
NSLog(@"setValue-forUndefinedKey--key:%@",key);
}
}
-(id)valueForUndefinedKey:(NSString *)key{
if([key isEqualToString:@"name"]){
NSLog(@"valueForUndefinedKey--key:%@",key);
return nil;
}
return @"1";
}
-(void)setNilValueForKey:(NSString *)key{
if([key isEqualToString:@"age"]){
NSLog(@"setNilValueForKey--key:%@",key);
[self setValue:@(18) forKey:@"age"];
}else{
[super setNilValueForKey:key];
}
}
@end
@interface ViewController12 ()
@end
@implementation ViewController12
- (void)viewDidLoad {
[super viewDidLoad];
Student_12 *stu = [[Student_12 alloc]init];
NSLog(@"======111========");
[stu setValue:@"1" forKey:@"name"];//setValue-forUndefinedKey--key:name
NSLog(@"======222========");
[stu valueForKey:@"name"];//valueForUndefinedKey--key:name
NSLog(@"======333========");
[stu setValue:@"2" forKeyPath:@"p.name"];//发现它只会 拦截stu里面是否有p属性 对于之后的 米有触发
NSLog(@"======444========");
[stu valueForKeyPath:@"p.name"];//发现它只会 拦截stu里面是否有p属性 对于之后的 米有触发
NSLog(@"=======555=======");
[stu setValue:nil forKey:@"age"];//有key,但是 给非对象的属性 设置nil时
}
/*
方法setValue:forKey:寻找不到指定Key或者KeyPath匹配的方法或变量名称会自动调用setValue:forUndefinedKey: 抛出NSUndefinedKeyException异常
方法valueForKey:寻找不到指定Key或者KeyPath匹配的方法或变量名称会自动调用valueForUndefinedKey: 抛出NSUndefinedKeyException异常
对非对象类型的属性设置nil空值,底层调用setNilValueForKey:,然后抛出NSInvalidArgumentException异常
*/
@end
2.键值验证
#import "ViewController13.h"
@interface Person_13 : NSObject
@property (nonatomic,strong)NSString *name;
@end
@implementation Person_13
@end
@interface Student_13 : NSObject
@property (nonatomic,strong)Person_13 *p;
@property (nonatomic,strong)NSString *number;
@property (nonatomic,strong)NSString *desStr;
@end
@implementation Student_13
-(BOOL)validateValue:(inout id _Nullable __autoreleasing *)ioValue forKey:(NSString *)inKey error:(out NSError * _Nullable __autoreleasing *)outError{
NSLog(@"%s--value:%@,inKey:%@",__FUNCTION__,*ioValue,inKey);
if([inKey isEqualToString:@"number"]||[inKey isEqualToString:@"p"]){
[self setValue:@"有值啦A" forKey:@"desStr"];
return YES;
}
*outError = [[NSError alloc]initWithDomain:@"不是p或者number属性" code:0 userInfo:nil];
return NO;
}
-(BOOL)validateValue:(inout id _Nullable __autoreleasing *)ioValue forKeyPath:(NSString *)inKeyPath error:(out NSError * _Nullable __autoreleasing *)outError{
NSLog(@"%s--value:%@,inKeyPath:%@",__FUNCTION__,*ioValue,inKeyPath);
if([inKeyPath isEqualToString:@"p.name"]){
[self setValue:@"有值啦B" forKey:@"desStr"];
return YES;
}
*outError = [[NSError alloc]initWithDomain:@"不是p的name属性" code:0 userInfo:nil];
return NO;
}
@end
//================================================
@interface ViewController13 ()
@end
@implementation ViewController13
- (void)viewDidLoad {
[super viewDidLoad];
Student_13 *stu = [[Student_13 alloc]init];
NSLog(@"=====111======");
NSError *error = nil;
NSString *value = @"1";
[stu validateValue:&value forKey:@"number1" error:&error];
NSLog(@"error1:%@",error);
NSLog(@"%@",[stu valueForKey:@"desStr"]);
NSLog(@"=====222======");
NSString *name = @"jack";
[stu validateValue:&name forKeyPath:@"p.name" error:&error];
NSLog(@"error2:%@",error);
NSLog(@"%@",[stu valueForKey:@"desStr"]);
}
/*
KVC提供一套API使得属性值生效。使得对象有机会接受值、提供默认值、拒绝新值、抛出错误原因。KVC不会自动调用,需要手动调用。默认实现过程:
1、调用validateValue:forKey:error:
2、在对象的方法列表中匹配validate<Key>:error:
3、如果找到则执行并返回结果
4、如果未找到则返回YES,并赋值
注意:set方法中禁止调用
*/
@end
3.非对象类型的处理
#import "ViewController14.h"
@interface ViewController14 ()
@property (nonatomic,assign)NSInteger age;
@property (nonatomic,assign)BOOL sex;
@property (nonatomic,assign)CGRect frame;
@end
@implementation ViewController14
- (void)viewDidLoad {
[super viewDidLoad];
//NSNumber对应的基本数据类型(如)
[self setValue:@"18" forKey:@"age"];
NSLog(@"%@",[[self valueForKey:@"age"] class]);//__NSCFNumber
[self setValue:@"0" forKey:@"sex"];
NSLog(@"%@",[[self valueForKey:@"sex"] class]);//__NSCFBoolean
//NSValue对应的结构体类型(如)
NSValue *value = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
[self setValue:value forKey:@"frame"];
NSLog(@"%@",[[self valueForKey:@"frame"] class]);//NSConcreteValue
}
/*
KVC对于基本数据类型和结构体在底层支持自动数据类型转换。根据相对的存取方法或者实例变量判端实际需要的值类型,选择NSNumber 或 NSValue 进行自动转换
*/
@end
友情链接: