zoukankan      html  css  js  c++  java
  • KVC和KVO的简单对比

    一、对于KVC模式(Key Value Coding):

    1、其实在实际开发中用得比较多得就是:接收到json数据之后,通过解析,解析成NSDictionary,然后再把字典对应的字段建立一个Model,在Model里面自定义一个类方法+(instancetype)modelWithDictionary:(NSDictionary *)keyDictionary方法中调用

    1
    [self setValuesForKeysWithDictionary:jsonObject];

    从而达到我们想要的效果,将字典装成Model。

    2、然后对于一些想要特殊处理的字段可以调用以下方法来进行特殊处理,比如里面有些key是字典类型,则可以通过以下方式搞定:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    -(void) setValue:(id)value forKey:(NSString *)key 
      if([key isEqualToString:@"products"]) 
      
        for(NSMutableDictionary *productDict in value) 
        
          Prodcut *product = [[Product alloc] initWithDictionary:prodcutDict]; 
          [self.products addObject:product]; 
        
      
    }

    3、还有一种情况,就是里面的Key压根就没定义,可以通过重写以下这个方法来重新把key和value对应上号:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key 
        if([key isEqualToString:@"nameXXX"]) 
            self.name = value; 
        if([key isEqualToString:@"ageXXX"]) 
            self.age = value; 
        else 
            [super setValue:value forKey:key]; 
    }

     

     

    二、对于KVO模式(Key-Value Observing):

    KVO概述:
    KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。
    简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。

    KVO的优点:
    当有属性改变,KVO会提供自动的消息通知。这样开发人员不需要自己去实现这样的方案:每次属性改变了就发送消息通知。
    这是KVO机制提供的最大的优点。因为这个方案已经被明确定义,获得框架级支持,可以方便地采用。
    开发人员不需要添加任何代码,不需要设计自己的观察者模型,直接可以在工程里使用。
    其次,KVO的架构非常的强大,可以很容易的支持多个观察者观察同 一个属性,以及相关的值。

    使用步骤如下:
    1. 注册,指定被观察者的属性,
    2. 实现回调方法
    3. 触发回调方法
    4. 移除观察

    KVO使用例子代码如下:
    ###############Model(模型)###############
    #import <Foundation/Foundation.h>
    @interface Music : NSObject {
        // 监听的属性
        NSString *musicName;
    }
    @end

    #import "Music.h"
    @implementation Music
    @end

    ###############ViewController(视图控制器)###############
    #import <UIKit/UIKit.h>
    @class Music;
    @interface ViewController : UIViewController {
        Music *music;   
    }
    @property (nonatomic, retain) Music *music;
    @end

    @implementation ViewController
    @synthesize music;

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
        [super viewDidLoad];
       
        music = [[Music alloc] init];
       
        // 添加观察者  注册当属性发生改变的时候被调用的
        [music addObserver:self forKeyPath:@"musicName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
       
        // UILabel控件
        UILabel *musicLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 280, 21)];
        musicLabel.font = [UIFont fontWithName:@"ArialMT" size:18];
        musicLabel.textColor = [UIColor redColor];
        musicLabel.tag = 100;
        [self.view addSubview:musicLabel];
        [musicLabel release];
       
        // UITextField控件
        UITextField *musicTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 280, 21)];
        musicTextField.font = [UIFont fontWithName:@"ArialMT" size:18];
        musicTextField.placeholder = @"Please enter some words.";
        musicTextField.backgroundColor = [UIColor whiteColor];
       
        // UITextField输入内容时候调用
        [musicTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
       
        [self.view addSubview:musicTextField];
        [musicTextField release];

        self.view.backgroundColor = [UIColor grayColor];
    }

    - (void)textFieldDidChange:(id)sender {
        UITextField *textField = (UITextField *)sender;
        NSLog(@">>>>>>>>>>>>>>>%@",textField.text);
        // 修改正在监听的属性,将调用下面回调方法
        [music setValue:textField.text forKey:@"musicName"];
    }

    // 只要Music类的"musicName"属性发生的变化都会触发到以下的方法
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        UILabel *label = (UILabel *)[self.view viewWithTag:100];
        // 如果改变的属性是"musicName"
        if ([keyPath isEqualToString:@"musicName"]) {
            // 将 当前的musicName属性的值 赋值给UILabel
            label.text = [music valueForKey:@"musicName"];
            // 输出改变前的值
            NSLog(@"old musicName is %@",[change objectForKey:@"old"]);
            // 输出改变后的值
            NSLog(@"new musicName is %@",[change objectForKey:@"new"]);        
        }
    }

    #pragma mark - Memory Management
    - (void)dealloc {
        // 移除观察者
        [music removeObserver:self forKeyPath:@"musicName"];
        [music release];
        [super dealloc];
    }

  • 相关阅读:
    007-搭建框架-开发AOP框架
    007-安装百度云,搜狗输入法,播放器
    006-重装yum
    005-快捷键,host,查看版本
    004-ibus输入法,快捷键,浏览器
    003-centos搭建idea开发java
    002-命令行模式安装图形界面
    006-网站统计中的数据收集原理及实现
    007-sql整体概述
    017-Hadoop Hive sql语法详解7-去重排序、数据倾斜
  • 原文地址:https://www.cnblogs.com/leiming1001/p/leiming001.html
Copyright © 2011-2022 走看看