标签:
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *_name; NSInteger _age; Person *_wife; } @end
#import "ViewController.h" #import "Person.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建对象 Person *person = [[Person alloc]init]; //使用KVC来存储对象的数据成员 [person setValue:@"Tom" forKey:@"_name"]; [person setValue:@25 forKey:@"_age"]; NSLog(@"person:%@",person); //取对象的数据成员 NSString *name = [person valueForKey:@"_name"]; NSInteger age = [[person valueForKey:@"_age"]integerValue]; NSLog(@"KVC方式存取:name:%@,age:%ld",name,age); } @end
如果需要打印输出对象,还需要重写Person类的descaprition方法。
#import "Person.h" @implementation Person -(NSString *)description { return [NSString stringWithFormat:@"name:%@,age:%ld",_name,_age]; } @end
3、扩展KVC的使用
Book类的.h文件 #import <Foundation/Foundation.h> @interface Book : NSObject @property(copy,nonatomic)NSString *bookName; @end Book类的.m文件 #import "Book.h" @implementation Book -(NSString *)description { return [NSString stringWithFormat:@"%@",_bookName]; } @end
Person类的.h文件 #import <Foundation/Foundation.h> @class Book; @interface Person : NSObject @property(copy,nonatomic)NSString *name; @property(assign,nonatomic)NSInteger age; @property(strong,nonatomic)Book *book; @end Person类的.m文件 #import "Person.h" @implementation Person -(NSString *)description { return [NSString stringWithFormat:@"name:%@,age:%ld",_name,_age]; } @end
最后是实现文件。
#import "ViewController.h" #import "Book.h" #import "Person.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Person *p1 = [[Person alloc]init]; [p1 setValue:@"Tom" forKey:@"name"]; [p1 setValue:@23 forKey:@"age"]; Book *book1 = [[Book alloc]init]; book1.bookName = @"iPhone"; p1.book = book1; Person *p2 = [[Person alloc]init]; [p2 setValue:@"Jerry" forKey:@"name"]; [p2 setValue:@23 forKey:@"age"]; Book *book2 = [[Book alloc]init]; book2.bookName = @"iOS"; p2.book = book2; NSLog(@"%@%@",p1,[p2 valueForKeyPath:@"name"]); NSArray *person = @[p1,p2]; NSLog(@"%@",person); NSArray *arrayM = [person valueForKeyPath:@"book.bookName"]; NSLog(@"%@",arrayM); } @end
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *_name; } @end
#import "Person.h" @implementation Person //接受被观察者发生变动后的通知 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"%@,%@,%@,%@",keyPath,object,change,context); } @end
再添加一个股票类,让Person作为观察者,对股票的价格进行监听。
#import <Foundation/Foundation.h> @interface Stock : NSObject { NSString *_name; float _price; } @end
实现文件内容:
#import "ViewController.h" #import "Person.h" #import "Stock.h" @interface ViewController () @property(strong,nonatomic)Person *person; @property(strong,nonatomic)Stock *stock; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建观察者 self.person = [[Person alloc]init]; [self.person setValue:@"Tom" forKey:@"_name"]; //创建股票 self.stock = [[Stock alloc]init]; [self.stock setValue:@"sxt" forKey:@"_name"]; [self.stock setValue:@1.2 forKey:@"_price"]; //设置观察者 [self.stock addObserver:self.person forKeyPath:@"_price" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:@"add"]; } //单击按钮让股票价格加1 - (IBAction)buttonClicked:(UIButton *)sender { float price = [[self.stock valueForKey:@"_price"]floatValue]; [self.stock setValue:@(price+1.0) forKey:@"_price"]; } -(void)dealloc { //删除观察者 [self.stock removeObserver:self.person forKeyPath:@"_price"]; } @end
标签:
原文地址:http://www.cnblogs.com/xjf125/p/4878750.html