iOS_KVC与KVO
- #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:@ 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:@ 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:@ 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
iOS_KVC与KVO的更多相关文章
- iOS---观察者模式之--->KVO
文章结构如下: Why? (为什么要用KVO) What? (KVO是什么) How? ( KVO怎么用) More (更多细节) 原理 自己实现KVO 在我的上一篇文章浅谈 iOS Notifica ...
- Objective-C之KVC、KVO
1,KVC(键值编码) Key Value Coding 1.1在C#中,可以通过字符串反射来获取对象,从而对对象的属性进行读写,Object-C中有同样的实现,通过字符串(属性名词)对对象的属性进 ...
- OS 如何选择delegate、notification、KVO?
原文链接:http://blog.csdn.net/dqjyong/article/details/7685933 前面分别讲了delegate.notification和KVO的实现原理,以及实际使 ...
- KVC 和 KVO
KVC 键值编码 全称是Key-value coding,翻译成键值编码.它提供了一种使用字符串而不是访问器方法去访问一个对象实例变量的机制. 1.通过key(成员变量的名称)设置 ...
- 11. KVC And KVO
1. KVC And KVO 的认识 KVC/KVO是观察者模式的一种实现 KVC全称是Key-value coding,翻译成键值编码.顾名思义,在某种程度上跟map的关系匪浅.它提供了一种使用 ...
- KVO __ 浅谈
KVO :Key-Value Observing 它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了. ...
- iOS开发系列--Objective-C之KVC、KVO
概述 由于ObjC主要基于Smalltalk进行设计,因此它有很多类似于Ruby.Python的动态特性,例如动态类型.动态加载.动态绑定等.今天我们着重介绍ObjC中的键值编码(KVC).键值监听( ...
- delegate、notification、KVO场景差别
delegate: 编译器会给出没有实现代理方法的警告 一对一 使用weak而不是assign,或者vc消失时置为nil 可以传递参数,还可以接收返回值 notification: 编译期无法排错 一 ...
- IOS学习之初识KVO
什么是KVO? KVO(Key-Value Observing)键值观察,是一种通过对对象的某一个属性添加观察者,一旦这个属性值发生变化,就会通知当前观察者的一种机制. 该如何使用? 1.注册,指定被 ...
随机推荐
- 006android初级篇之jni数据类型映射
JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++) 使用JNI的副作用 一旦使用JNI,JAVA程序就丧失了JAV ...
- linux c 常常混淆的概念
指针函数 and 函数指针 指针函数是指带指针的函数,即本质是一个函数.函数都有返回类型(假设不返回值,则为无值型),仅仅只是指针函数返回类型是某一类型的指针. 定义格式 类型名 *函数名(函数參数列 ...
- ios -完全实现代码设置 Could not find a storyboard named 'Main' in bundle NSBundle
UIWindow *windows = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; windows.background ...
- Golang中的正则表达式
声明:文章转自GoLove 用法: 单一: . 匹配任意一个字符,如果设置 s = true,则可以匹配换行符 [字符类] 匹配"字符类"中的一个字符,"字符类" ...
- go生成xml
package main import ( "encoding/xml" "fmt" // "os" ) type Servers stru ...
- aspx.cs方法设置webmenthod特性接收ajax请求
cs代码: public partial class TelerikWebMethod : BasePage//System.Web.UI.Page { protected void Page_Loa ...
- 关于angularjs中的ng-class的使用
在angularjs中,包子认为ng-class的用法是相当的大,这个相当于jquery的addClass和removeClass.在这个地方,废话不多说,直接上一小段代码 当kefu这个变量为真的时 ...
- 距离,margin padding ,width height 用法 ,记录
margin:0 auto 表示什么意思 margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right 因为0 auto,表示上下边界为0,左右则根据宽度自适应 ...
- Linux 入门介绍
背景:最近在搞redis 集群 ,然后有时候会怀疑自己,那么问题来了, 怀疑自己就是自己不扎实! 记忆不好了! 写下来备份吧! 才入门时候总是会想 ,Linux 下面文件夹都是什么意思! bin ...
- 64位matlab mex64位编译器解决方案
安装libsvm的时候用到了mex -setup,有的会报 Could not find the 64-bit compiler. This may indicate that the "X ...