KVC

作者: 本泽马 | 来源:发表于2017-10-16 08:30 被阅读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;

    }

    相关文章

      网友评论

          本文标题:KVC

          本文链接:https://www.haomeiwen.com/subject/ibohuxtx.html