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.注册,指定被 ...
随机推荐
- 嵌入式开发之davinci--- 8148/8168/8127 中swms、Mosaic’s、display 显示pal 模式
(1) (2) (3) (4) -------------------------author:pkf ------------------------------time:2-3 --------- ...
- java字符编码详解
引用自:http://blog.csdn.net/jerry_bj/article/details/5714745 GBK.GB2312.iso-8859-1之间的区别 GB2312,由中华人民共和国 ...
- Android-ViewPagerIndicator框架使用——TitlePageIndicator
前言:TitlePageIndicator这个就是效果比较好. 一:定义布局文件simple_titles: <LinearLayout xmlns:android="http://s ...
- ios --图片文字组合头像那点事
/** 图片文字组合头像那点事 @param string 昵称 @param imageSize 图片尺寸 @param imageColor 图片颜色 @return 返回的 图片 */ + (U ...
- 那些可爱的 Linux 命令
环境 root@15b883:~# uname -a ##需要是Ubuntu环境 Linux 15b883 --generic #- :: UTC x86_64 x86_64 x86_64 GNU/L ...
- Linux命令之split
split用来将大文件分割成小文件.有时文件越来越大,传送这些文件时,首先将其分割可能更容易. 使用vi或其他工具诸如sort时,如果文件对于工作缓冲区太大,也会存在一些问题. 因此有时没有选择余地, ...
- 3N Numbers
D - 3N Numbers Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Let N b ...
- Hystrix 基于注解开发
不对地方,请指出!相互学习! 背景:Hystrix 没有无参构造函数,所以Spring管理bean时候没办法进行管理, 每个类都进行编码 个人感觉不方便,基于注解开发!方便速度快,不侵入代码!引入的j ...
- Xcode生成ipa文件
想到蒲公英应用做分发测试的同学们可以用的到哈 在测试的Device中切换为IOS Device,选择当前项目修改BuildSeting 在code signing Identity中选择证书,没有测试 ...
- 路径规划 Adjacency matrix 传球问题
建模 问题是什么 知道了问题是什么答案就ok了 重复考虑 与 重复计算 程序可以重复考虑 但往目标篮子中放入时,放不放把握好就ok了. 集合 交集 并集 w 路径规划 字符串处理 42423 424 ...