KVC

来源:互联网 发布:鸿合录播软件 编辑:程序博客网 时间:2024/05/16 04:27

1、先建一个Person类和Book类

@class Book;


@interface Person :NSObject <NSCoding>


@property (strong,nonatomic)NSString *name;

@property (assign,nonatomic)NSInteger age;

@property (assign,nonatomic)double height;


@property (strong,nonatomic)Book *book;


@end


@interface Book : NSObject


@property (strong,nonatomic)NSString *bookName;

@property (assign,nonatomic)double price;


@end


2、在别的.m文件中使用

    self.person = [[Personalloc]init];

    self.person.book = [[Bookalloc]init];

    

    [self.personsetValue:@"lili"forKey:@"name"];

    [self.personsetValue:@"20"forKey:@"age"];

    [self.personsetValue:@"200"forKey:@"height"];

    

    [self.personsetValue:@"ios"forKeyPath:@"book.bookName"];

    [self.personsetValue:@"11.5"forKeyPath:@"book.price"];

    

    NSLog(@"%@",self.person.book);



0 0
原创粉丝点击