////
//// main.m
//// TestBasis
////
//// Created by ficow on 16/1/14.
//// Copyright © 2016年 ficow. All rights reserved.
////
//
#import <Foundation/Foundation.h>
///////////////////////////////////////////////////////////////////////////////////////////////
@interface myClass1 : NSObject
@property (nonatomic, copy) NSString *str;
@end @implementation myClass1
@end
///////////////////////////////////////////////////////////////////////////////////////////////
@interface myClass : NSObject
{
}
@property (nonatomic, retain) myClass1 *item;
@end @implementation myClass - (id)init{
if(self = [super init]){
[self setValue:[[myClass1 alloc]init] forKey:@"item"];//如果不给item设置值,则最后得到的item.str为空!
}
return self;
}
/////////////////////////////////////////////////////////////////////////////////////////////
//KVO part
- (void) setItem:(myClass1 *)item{
_item = item;
[self addObserver:self forKeyPath:@"item.str" options:NSKeyValueObservingOptionNew context:nil];//1.添加监听器,监听item.str
//[self.item addObserver:self forKeyPath:@"str" options:NSKeyValueObservingOptionNew context:nil];和上面的效果一样
} //重写监听的方法,当被监听的对象改变了,就回调监听器的这个方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"item.str has changed");
} - (void)dealloc{
[self removeObserver:self forKeyPath:@"item.str"];//用完就删除监听器
} @end
///////////////////////////////////////////////////////////////////////////////////////////// int main(int argc, const char * argv[]) {
@autoreleasepool {
class1.item.str = @"item.str";
}
return ;
}

最新文章

  1. Python爬虫 网页图片
  2. SQL Server远程连接操作
  3. jstack(查看线程)、jmap(查看内存)和jstat(性能分析)
  4. C# 制作卸载文件
  5. cocos2d的安装
  6. xunsearch安装与卸载
  7. spring boot 配置文件application
  8. 是否可能两个ETH私钥对应同一个地址
  9. 小白的Python之路_day1
  10. consul配置和使用
  11. Educational Codeforces Round 23 F. MEX Queries 离散化+线段树
  12. Nordic SDK例程目录结构
  13. Flex DateTime Format
  14. 大型web系统数据缓存设计-l转载
  15. Strut2------获取界面返回的session,application,parameter
  16. 一键安装lnmp-nginx(3)
  17. Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】
  18. Amixer 控制声音
  19. UVa 12377 - Number Coding
  20. 【CV知识学习】【转】beyond Bags of features for rec scenen categories。基于词袋模型改进的自然场景识别方法

热门文章

  1. yii压缩
  2. mysql TIMESTAMP 报错
  3. java课后作业7
  4. java课后作业5
  5. .net学习笔记---xml基础知识
  6. Feature hashing相关 - 2
  7. 深入理解JavaScript定时机制和定时器注意问题
  8. Win10 兼容性 Visual studio web应用程序 ASP.NET 4.0 尚未在 Web 服务器上注册
  9. HTML页面实现返回顶部效果 go to top
  10. MyEclipse 关闭鼠标悬停提示