KVC使用
==================User===================
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic,copy)NSString* name;
@property (nonatomic,copy)NSString* pass;
@property (nonatomic,copy)NSDate *birth;
@end
User.m
#import "User.h"
@implementation User
@synthesize name,pass,birth;
@end
处理不存在的key和nil
==================Apple===================
Apple.h
#import <Foundation/Foundation.h>
@interface Apple : NSObject
@property (nonatomic,strong) NSString*brand;
@property (nonatomic,assign) double price;
@end
Apple.m
#import "Apple.h"
@implementation Apple
@synthesize price,brand;
//处理设置key不存在问题
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"您尝试设置的key:【%@】并不存在",key);
//NSLog(@"您尝试设置的key:【%@】并不存在",key);
}
//处理访问key不存在问题
-(id)valueForUndefinedKey:(NSString *)key{
NSLog(@"您尝试访问的key:【%@】并不存在",key);
return nil;
}
//处理设置空值问题
-(void)setNilValueForKey:(NSString *)key{
if ([key isEqualToString:@"price"]) {
//将该price设置为0
price = 0;
}else{
[super setNilValueForKey:key];
}
}
@end
key路径
==================Order===================
#import <Foundation/Foundation.h>
@class Item;
@interface Order : NSObject
@property(nonatomic,strong) Item*item;
@property(nonatomic,assign) int amount;
@end
#import "Order.h"
@implementation Order
@synthesize amount;
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"");
}
-(id)valueForUndefinedKey:(NSString *)key{
return nil;
}
-(void)setNilValueForKey:(NSString *)key{
NSLog(@"");
}
@end
==================Item===================
#import <Foundation/Foundation.h>
@interface Item : NSObject
@property (nonatomic,strong) NSString* name;
@property (nonatomic,assign) int age;
@end
#import "Item.h"
@implementation Item
@synthesize age,name;
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"");
}
-(id)valueForUndefinedKey:(NSString *)key{
return nil;
}
-(void)setNilValueForKey:(NSString *)key{
NSLog(@"");
}
@end
==================Main.m===================
#import <Foundation/Foundation.h>
#import "User.h"
#import "Dog.h"
#import "Apple.h"
#import "Order.h"
#import "Item.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// User *user = [[User alloc]init];
// [user setValue:@"张三" forKey:@"name"];
// [user setValue:@"1234" forKey:@"pass"];
// [user setValue:[[NSDate alloc]init] forKey:@"birth"];
//
// NSLog(@"name=%@",[user valueForKey:@"name"]);
// NSLog(@"pass=%@",[user valueForKey:@"pass"]);
// NSLog(@"age=%@",[user valueForKey:@"birth"]);
// Dog* dog = [[Dog alloc]init];
// [dog setValue:@"旺财" forKey:@"name"];
// NSLog(@"dog->name:%@",dog->name);
// //NSLog(@"dog->_name:%@",dog->_name);
// [dog setValue:[NSNumber numberWithInt:20] forKey:@"age"];
// NSLog(@"age:%@",[dog valueForKey:@"age"]);
// Apple* apple = [[Apple alloc]init];
// [apple setValue:@"苹果" forKey:@"name"];
// [apple valueForKey:@"name"];
//
// [apple setValue:nil forKey:@"brand"];
// [apple setValue:nil forKey:@"price"];
Order* order = [[Order alloc]init];
[order setValue:@"12" forKey:@"amount"];
[order setValue:[[Item alloc]init] forKey:@"item"];
[order setValue:@"张三" forKeyPath:@"item.named"];
[order setValue:[NSNumber numberWithInt:20] forKeyPath:@"item.age"];
NSLog(@"name=%@",[order valueForKeyPath:@"item.name"]);
NSLog(@"age=%@",[order valueForKeyPath:@"item.age"]);
}
return 0;
}