zoukankan      html  css  js  c++  java
  • ios开发--KVO浅析

     

    目标:监听NSMutableArray对象中增加了什么

    代码如下:

    C代码 复制代码 收藏代码
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.   
    5.     self.dataArray = [NSMutableArray arrayWithObject:@"1"];  
    6.     [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];  
    7.        
    8. }  
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.dataArray = [NSMutableArray arrayWithObject:@"1"];
        [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
         
    }
    C代码 复制代码 收藏代码
    1. - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context  
    2. {  
    3.     NSLog(@"%@", keyPath);  
    4.     NSLog(@"%@", object);  
    5.     NSLog(@"%@", change);  
    6. }  
    - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        NSLog(@"%@", keyPath);
        NSLog(@"%@", object);
        NSLog(@"%@", change);
    }
    C代码 复制代码 收藏代码
    1. - (IBAction)add:(id)sender  
    2. {  
    3.     NSArray *addData = [NSArray arrayWithObjects:@"11", @"12", @"13", nil];  
    4.     [self.dataArray addObjectsFromArray:addData];  
    5.       
    6.     self.dataArray = [NSMutableArray arrayWithObject:@"2"];  
    7. }  
    - (IBAction)add:(id)sender
    {
        NSArray *addData = [NSArray arrayWithObjects:@"11", @"12", @"13", nil];
        [self.dataArray addObjectsFromArray:addData];
        
        self.dataArray = [NSMutableArray arrayWithObject:@"2"];
    }

    输入日志:

    C代码 复制代码 收藏代码
    1. 2013-01-15 16:05:10.120 KVOTest[2199:907] dataArray  
    2. 2013-01-15 16:05:10.121 KVOTest[2199:907] <ZZTViewController: 0x20846590>  
    3. 2013-01-15 16:05:10.123 KVOTest[2199:907] {  
    4.     kind = 1;  
    5.     new =     (  
    6.         2  
    7.     );  
    8.     old =     (  
    9.         1,  
    10.         11,  
    11.         12,  
    12.         13  
    13.     );  
    14. }  
    2013-01-15 16:05:10.120 KVOTest[2199:907] dataArray
    2013-01-15 16:05:10.121 KVOTest[2199:907] <ZZTViewController: 0x20846590>
    2013-01-15 16:05:10.123 KVOTest[2199:907] {
        kind = 1;
        new =     (
            2
        );
        old =     (
            1,
            11,
            12,
            13
        );
    }

    经过测试得如下结论:kvo监听的是对象指针的变动,NSString、int、float等对象的变动(abc = @"123"、abc = 12、abc = 12.2)皆为指针的变动,所以通过此方式来捕捉array的变化是不可行的

    但,我们可以通过此方式来做控件属性的变动。如下:

    C代码 复制代码 收藏代码
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.       
    5.     self.personObject = [PersonObject personObjectWithBankInstance:[BankObject bankObjectWithAccountBalance:10]];  
    6.       
    7.     [self.personObject addObserver:self forKeyPath:@"bankInstance.accountBalance" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];    // 此处注意是监听personObject对象下的bankInstance的accountBalance变化  
    8. }  
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.personObject = [PersonObject personObjectWithBankInstance:[BankObject bankObjectWithAccountBalance:10]];
        
        [self.personObject addObserver:self forKeyPath:@"bankInstance.accountBalance" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];    // 此处注意是监听personObject对象下的bankInstance的accountBalance变化
    }
    C代码 复制代码 收藏代码
    1. - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context  
    2. {  
    3.     NSLog(@"%@", keyPath);  
    4.     NSLog(@"%@", object);  
    5.     NSLog(@"%@", change);  
    6. }  
    - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        NSLog(@"%@", keyPath);
        NSLog(@"%@", object);
        NSLog(@"%@", change);
    }
    C代码 复制代码 收藏代码
    1. - (IBAction)add:(id)sender  
    2. {  
    3.     [self.personObject.bankInstance setAccountBalance:2111];  
    4. }  
    - (IBAction)add:(id)sender
    {
        [self.personObject.bankInstance setAccountBalance:2111];
    }

     输出日志:

    C代码 复制代码 收藏代码
    1. 2013-01-15 16:05:10.111 KVOTest[2199:907] bankInstance.accountBalance  
    2. 2013-01-15 16:05:10.116 KVOTest[2199:907] <PersonObject: 0x20856180>  
    3. 2013-01-15 16:05:10.118 KVOTest[2199:907] {  
    4.     kind = 1;  
    5.     new = 2111;  
    6.     old = 10;  
    7. }  
    2013-01-15 16:05:10.111 KVOTest[2199:907] bankInstance.accountBalance
    2013-01-15 16:05:10.116 KVOTest[2199:907] <PersonObject: 0x20856180>
    2013-01-15 16:05:10.118 KVOTest[2199:907] {
        kind = 1;
        new = 2111;
        old = 10;
    }

    如有问题,请留言共同探讨。

  • 相关阅读:
    sass和less的对比
    vue 源码分析
    vue的全家桶
    Vue组件化和路由
    开发技术文档汇总
    NodeJs前端构建工具 ——————之Grunt篇
    grunt使用小记之uglify:最全的uglify使用DEMO
    20 种提升网页速度的技巧
    webfont应用系列(二)如何制作图标字体?
    快速上手制作Icon Font
  • 原文地址:https://www.cnblogs.com/lovewx/p/4210737.html
Copyright © 2011-2022 走看看