KVC

2017-10-16  本文已影响0人  本泽马

#import#import "Dog.h"

@interface Person : NSObject

@property(nonatomic,strong)Dog *dog;

@end

#import "Person.h"

@interface Person ()

@property(nonatomic,strong)NSString *sex;

@end

@implementation Person

-(NSString *)description

{

return [NSString stringWithFormat:@"sex === %@",_sex];

}

-(void)setValue:(id)value forUndefinedKey:(nonnull NSString *)key

{

NSLog(@"你给不存在的属性赋值了");

}

-(id)valueForUndefinedKey:(NSString *)key

{

return @"没有你要取的属性";

}

#import@interface Dog : NSObject

@property(nonatomic,strong)NSString *name;

@property(nonatomic,strong)NSString *age;

@end

#import#import "Person.h"

#import "Dog.h"

int main(int argc, const char * argv[]) {

@autoreleasepool {

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

[person setValue:@"北京" forKey:@"sex"];

NSLog(@"%@",[person valueForKey:@"sex"]);

[person setValue:@"北京" forKey:@"aaa"];

NSLog(@"%@",[person valueForKey:@"aaa"]);

person.dog = [[Dog alloc]init];

[person setValue:@"aaa" forKeyPath:@"dog.name"];

NSLog(@"%@",[person valueForKeyPath:@"dog.name"]);

}

return 0;

}

上一篇 下一篇

猜你喜欢

热点阅读