iOS KVC KVO 代码 详解
2016-04-07 15:09
423 查看
// // ViewController.m // test_kvo_kvc_01 // // Created by admin on 4/7/16. // Copyright © 2016 jeffasd. All rights reserved. // #import "ViewController.h" #import "Persion.h" //#import <obj/runtime> static NSString * const TESTSTRING = @"123"; #define KVOSTRING @"321" @interface ViewController () @property (nonatomic, strong, readonly) NSString *viewName; @property (nonatomic, strong, readonly) Persion *xiaozhan; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self testForKVO]; } - (void)testForKVO{ _xiaozhan = [Persion new]; [_xiaozhan addObserver:self forKeyPath:@"personName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:KVOSTRING ]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ NSLog(@"the context is %@", context); if (context == KVOSTRING) { NSLog(@"kvo"); return; }else{ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } - (void)removeObservation{ [_xiaozhan removeObserver:self forKeyPath:@"personName"]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [_xiaozhan setValue:[NSNumber numberWithInt:12] forKey:@"age"]; // _xiaozhan.age = 122; _xiaozhan.personName = @"123"; // [_xiaozhan setPersonName:@"xiaozhan"]; } - (IBAction)buttonAction:(UIButton *)sender { [self removeObservation]; } - (void)testForKVC{ Persion *xiaoming = [Persion new]; // [xiaoming setValue:@"xiaoming1" forKey:@"name"]; // [xiaoming setValue:nil forKey:@"name"]; [xiaoming setValue:nil forKey:@"age"]; // [xiaoming setValue:[NSNumber numberWithInt:12] forKey:@"name"]; // [xiaoming setValue:[NSNumber numberWithInt:12] forKey:@"name1"]; //还有一个需要注意的是KVC 并没有类型检验,毕竟Object-C 还是动态的啦 NSLog(@"the name is %@", xiaoming.name); NSLog(@"the class name is %@", [xiaoming.name class]); // ViewController *VC = [ViewController new]; // [VC setValue:@"123" forKey:@"viewName"]; } - (BOOL)validateValue:(inout id _Nullable __autoreleasing *)ioValue forKey:(NSString *)inKey error:(out NSError * _Nullable __autoreleasing *)outError{ return NO; } - (nullable id)valueForUndefinedKey:(NSString *)key{ return @"123"; } - (void)setNilValueForKey:(NSString *)key{ } - (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key{ // NSLog(@"the value is %@", value); // NSLog(@"the key is %@", key); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
// // Persion.h // test_kvo_kvc_01 // // Created by admin on 4/7/16. // Copyright © 2016 jeffasd. All rights reserved. // #import <Foundation/Foundation.h> @interface Persion : NSObject @property (nonatomic, strong, readonly) NSString *name; @property (nonatomic, assign) NSUInteger age; @property (nonatomic, strong) NSString *personName; @end
// // Persion.m // test_kvo_kvc_01 // // Created by admin on 4/7/16. // Copyright © 2016 jeffasd. All rights reserved. // #import "Persion.h" #define KVOSTRING @"personName" @interface Persion () @property (nonatomic, strong)NSString *identifier; @end @implementation Persion //- (BOOL)validateName:(inout id _Nullable __autoreleasing *)ioValue forKey:(NSString *)inKey error:(out NSError * _Nullable __autoreleasing *)outError{ // // NSLog(@"ioValue is %@", ioValue); // // NSLog(@"inkey is %@", inKey); // // return NO; //} - (BOOL)validateValue:(inout id _Nullable __autoreleasing *)ioValue forKey:(NSString *)inKey error:(out NSError * _Nullable __autoreleasing *)outError{ return NO; } - (nullable id)valueForUndefinedKey:(NSString *)key{ return @"valueUndefinedKey"; } - (void)setNilValueForKey:(NSString *)key{ NSLog(@"the key is %@", key); } - (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key{ } //- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ // // NSLog(@"the context is %@", context); // // if (context == KVOSTRING) { // NSLog(@"kvo"); // }else{ // [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; // } //} @end
相关文章推荐
- ios 开源代码
- IOS开发-表单控件的应用
- IOS-处理异常崩溃(摘自iPhone Tutorials)
- iOS开发中的错误整理,导航控制器的导航栏取消系统渲染的错误
- IOS-线程异步操作GCD
- iOS-如何让xcode自动检查内存泄露
- ios-调试程序打印详细信息 文件名 函数名 行号
- ios-NSString总结
- iOS 开发调试技巧总结
- ios-拉伸图片变形解决办法:选取某一区域
- iOS开发小技巧--取消按钮的选中状态
- iOS 9.3 升级后无法真机测试 Could not find Developer Disk Image
- iOS彩票项目--第一天,自定义TabBar控制器和自定义TabBar,自定义导航控制器
- 图片下拉缩放效果
- iOS绘制 - 自定义制作进度条
- iOS开发出错whose view is not in the window hierarchy!的解决
- iOS开发出错whose view is not in the window hierarchy!的解决
- iOS开发出错whose view is not in the window hierarchy!的解决
- iOS字体笔记
- iOS:状态栏提示控件的实现原理