#import "ViewController.h" #import "Person.h" #import "Student.h" @interface ViewController () { NSString *_text; } @property (weak, nonatomic) IBOutlet UITextField *plicceholder; @property (nonatomic, strong) Person *person; @property (nonatomic, strong) NSMutableArray *tempArray; @end @implementation ViewController //KVC key -Value - Coding //KVO ,key - value - observer,键值观察,属性只要调用set方法就会触发KCO - (void)viewDidLoad { [super viewDidLoad]; [self.plicceholder setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; //属性初始化,必须使用self.进行调用. self.person = [[Person alloc] init]; //1.添加观察者 //2.self.person是被观察的对象本身 //self,观察者 //keyPath,观察的实例变量 //options ,观察哪方面 [self.person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil]; [self.person addObserver:self forKeyPath:@"_age" options:NSKeyValueObservingOptionNew context:nil]; [self addObserver:self forKeyPath:@"_text" options:NSKeyValueObservingOptionNew context:nil]; self.tempArray = [@[] mutableCopy]; [self addObserver:self forKeyPath:@"tempArray" options:NSKeyValueObservingOptionNew context:nil]; } - (IBAction)kvo_fire:(id)sender { //self.person.name = @"张浩浩"; [self.person setValue:@"18" forKey:@"_age"]; } //接收kvo触发事件 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"object = %@,keyPath = %@", object,keyPath); // NSLog(@"%@", keyPath); } - (IBAction)kvc_setValueForKey:(id)sender { Person *p = [[Person alloc] init]; [p setValue:@"豪豪" forKey:@"name"]; NSLog(@"%@", p.name); [p setValue:@"18" forKey:@"_age"]; NSLog(@"%@",[p valueForKey:@"_age"]); } - (IBAction)kvc_setValueForPath:(id)sender { Student *stu =[[Student alloc] init]; // Person *p = [[Person alloc] init]; // stu.per = p; [stu setValue:@"豪豪" forKeyPath:@"per.age"]; NSLog(@"%@", [stu valueForKeyPath:@"per.age"]); } - (IBAction)kvc_setValuserForkeysWithDictionary:(id)sender { NSDictionary *dic = @{@"name":@"张豪豪",@"gender":@"男"}; Person *p = [[Person alloc] init]; [p setValuesForKeysWithDictionary:dic]; NSLog(@"%@", p.name); } - (IBAction)kvc_sum:(id)sender { NSMutableArray *ary = [@[] mutableCopy]; for (int i = 0; i < 100; i++) { [ary addObject:[NSString stringWithFormat:@"%d", i]]; } NSLog(@"%@", [ary valueForKeyPath:@"@sum.integerValue"]); } - (IBAction)kvo_observerself:(id)sender { [self setValue:@"biubiu" forKey:@"_text"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc{ //观察着不需要的时候,就需要进行注销,并不是绝对的在dealloc里面 [self.person removeObserver:self forKeyPath:@"name"]; [self.person removeObserver:self forKeyPath:@"_age"]; [self removeObserver:self forKeyPath:@"_text"]; } - (IBAction)kvo_mutableArray:(id)sender { //kvo 观察数组 [[self mutableArrayValueForKey:@"tempArray"] addObject:@"zhaoahao"]; } @end
Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *_age; } @property (nonatomic, copy) NSString *name; @end
person.m
#import "Person.h" @implementation Person //属性关联实例变量 @synthesize name = _name; - (void)setValue:(id)value forUndefinedKey:(NSString *)key{ } @end
student.h
#import <Foundation/Foundation.h> @class Person; @interface Student : NSObject @property (nonatomic, strong) Person *per; @end
student.m
#import "Student.h" #import "Person.h" @implementation Student - (id)init{ self = [super init]; if (self) { self.per = [[Person alloc] init]; } return self; } @end