IOS学习笔记七之KVC和Key路径
1、KVC介绍
1)、KVC是由NSKeyValueCoding协议提供支持最基本的属性和两个方法如下
setValue:属性值 forkey:属性名:为指定属性设置值
valueForKey:属性名 (得到或者指定属性的值)
2)、当设置value为nil的时候,我们需要在类的实现里面重写setNilValueForKey方法,不然会抛出NSInvalidArgumentException
2、key路径介绍
KVC除了操作对象的属性之外,还可以操作对象的“复合属性”,比如类里面的成员变量是对象,然后给这个成员变量的对象进行赋值,就这样简单理解,赋值之前一定要记得把这个对象进行初始化操作
KVC协议中为操作Key路径的方法如下
setValue:forKeyPath:根据Key设置属性值
valueForKeyPath:根据key的路径获取属性值
2、测试简单Demo
User.h
#ifndef User_h
#define User_h
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic) NSString *name;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, copy) NSString *add;
@property NSString *pass;
@property NSDate *birth;
@property NSDate *birth1;
@end
#endif /* User_h */
User.m
#import <Foundation/Foundation.h>
#import "User.h"
@implementation User
@synthesize name = _name;
@synthesize pass;
@synthesize birth;
-(void) setName:(NSString *)name
{
self->_name = [NSString stringWithFormat:@"hello%@", name];
}
@end
KVCPerson.h
#ifndef KVCPerson_h
#define KVCPerson_h
#import <Foundation/Foundation.h>
#import "User.h"
@interface KVCPerson : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *pass;
@property (nonatomic, copy) NSDate *birth;
@property (nonatomic) int price;
@property (nonatomic) User *user;
@end
#endif /* KVCPerson_h */
KVCPerson.m
#import <Foundation/Foundation.h>
#import "KVCPerson.h"
@implementation KVCPerson
-(void)setNilValueForKey:(id)key
{
if ([key isEqualToString:@"price"])
{
_price = 0;
}
else
{
[super setNilValueForKey:key];
}
}
@end
main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Person.h"
#import "Apple.h"
#import "User.h"
#import "Args.h"
#import "KVCPerson.h"
#import "FKItem.h"
#import "FKOrder.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
KVCPerson *person = [KVCPerson new];
[person setValue:@"chenyu" forKey:@"name"];
[person setValue:@"1234" forKey:@"pass"];
[person setValue:[NSDate date] forKey:@"birth"];
NSLog(@"name is %@", [person valueForKey:@"name"]);
NSLog(@"pass is %@", [person valueForKey:@"pass"]);
NSLog(@"birth is %@", [person valueForKey:@"birth"]);
NSLog(@"price is %@", [person valueForKey:@"price"]);
[person setValue:nil forKey:@"name"];
[person setValue:nil forKey:@"price"];
NSLog(@"name is %@", [person valueForKey:@"name"]);
NSLog(@"pass is %@", [person valueForKey:@"pass"]);
NSLog(@"birth is %@", [person valueForKey:@"birth"]);
NSLog(@"price is %@", [person valueForKey:@"price"]);
[person setValue:[User new] forKeyPath:@"user"];
//在使用valueForKeyPath之前一定要记得把成员对象进行初始化,不然直接设置无效
[person setValue:@"hello" forKeyPath:@"user.city"];
NSLog(@"user.city is%@", [person valueForKeyPath:@"user.city"]);
}
}
3、运行结果
name is chenyu
pass is 1234
birth is Fri Jul 6 22:39:34 2018
price is 0
name is (null)
pass is 1234
birth is Fri Jul 6 22:39:34 2018
price is 0
user.city is hello
4、总结
在使用key的时候,一定要记得先把成员属性的对象进行初始化,不然设置无效
如下
[person setValue:[User new] forKeyPath:@"user"];
//在使用valueForKeyPath之前一定要记得把成员对象进行初始化,不然直接设置无效
[person setValue:@"hello" forKeyPath:@"user.city"];
NSLog(@"user.city is%@", [person valueForKeyPath:@"user.city"]);
而不是
[person setValue:@"hello" forKeyPath:@"user.city"];
NSLog(@"user.city is%@", [person valueForKeyPath:@"user.city"]);
这样结果会是null
作者:chen.yu
深信服三年半工作经验,目前就职游戏厂商,希望能和大家交流和学习,
微信公众号:编程入门到秃头 或扫描下面二维码
零基础入门进阶人工智能(链接)