美文网首页iOS精華
Swift3.0 KVC & KVO

Swift3.0 KVC & KVO

作者: erU | 来源:发表于2016-09-24 12:35 被阅读1579次

KVC

key-value coding。

  • 是一种间接访问对象的机制。
  • key的值就是属性名称的字符串,返回的value是任意类型,需要自己转化为需要的类型。
  • KVC主要就是两个方法。

    通过key设置对应的属性。
    通过key设置对应的属性。

🌰

class KVCDemo:NSObject{
    var data = "hello world"
}
var instance = KVCDemo()
var value = instance.value(forKey: "data") as! String
instance.setValue("hello hhy",forKey:"data")

print(instance.data)//

KVO

key-value observing。

  • 建立在KVC之上的的机制。
  • 主要功能是检测对象属性的变化。
  • 这是一个完善的机制,不需要用户自己设计复杂的视察者模式。
  • 对需要视察的属性要在前面加上dynamic关键字。

🌰

  1. 对要视察的对象的属性加上dynamic关键字。
class KVODemo:NSObject{
    dynamic var demoDate = NSDate()
    func updateDate(){
        demoDate = NSDate()
    }
}
  1. 声明一个全局的用来辨别是哪一个被视察属性的变量。
    private var mycontext = 0

  2. 在要视察的类中addObserver,在析构中removeObserver,重写observeValue

data.addObserver(self, forKeyPath: "demoDate", options: .new, context: &mycontext)
    deinit {
         data.removeObserver(self,forKeyPath:"demoDate")
    }
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        print("\(context!)============\(mycontext)")
        if(context == &mycontext) {
            print("Changed to:(change[NSKeyValueChangeNewKey]!)")
        }
        
    }
@IBAction func button(_ sender: AnyObject) {
        data.updateDate()
    }

--

完整代码

import Foundation
class KVODemo:NSObject{
    dynamic var demoDate = NSDate()
    func updateDate(){
        demoDate = NSDate()
    }
}
import UIKit

private var mycontext = 0

class ViewController: UIViewController {
   
 var data = KVODemo()

    override func viewDidLoad() {
        super.viewDidLoad()
         //1.注册监听对象                         
        data.addObserver(self, forKeyPath: "demoDate", options: .new, context: &mycontext)
//forKeyPath: "demoDate"     - 需要监听的属性
//options: .new              - 指返回的字典包含新值
//options: .old              - 指返回的字典包含旧值。
//context: &mycontext        -context方便传输你需要的数据,它是个指针类型
    }

    deinit {
         data.removeObserver(self,forKeyPath:"demoDate")
    }
   //2. 实现监听方法
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        print("\(context!)============\(mycontext)")
        if(context == &mycontext) {
            print("Changed to:(change[NSKeyValueChangeNewKey]!)")
        }
    }
   
    @IBAction func button(_ sender: AnyObject) {
        data.updateDate()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

相关文章

  • Swift3.0 KVC & KVO

    KVC key-value coding。 是一种间接访问对象的机制。 key的值就是属性名称的字符串,返回的va...

  • KVC && KVO

    1.KVC 关于KVC和KVO,我之前的总结文章有写过,但是实际上我在平日工作里,KVC和KVO使用的相对较少,不...

  • KVO & KVC

    一、KVO 二、KVCkey value coding : 间接通过字符串类型的key取出对应的属性值

  • KVO & KVC

    KVO: KVO的全称是Key-Value Observing,俗称“键值监听”,可以用于监听某个对象属性值的改变...

  • KVC & KVO

    KVC(Key-Value Coding) KVC最常见的两个用途: 给私有的成员变量赋值(当然公有的也可以)比如...

  • KVC & KVO

    平时用 KVO 用的少啊, 基本上都是不用的那种, 以至于怎么用都不知道了。晚上下班回来,还是决定要恶补啊,毕竟看...

  • KVC & KVO

    KVC - Key Value Coding 键值编码 一、KVC 简介 KVC 是一种可以直接通过字符串 key...

  • KVC、KVO

    KVC、KVO探识(一)KVO和KVO的详细使用 KVC、KVO探识(二)KVC你不知道的东西 KVC、KVO探识...

  • KVC,KVO

    KVC , KVO KVC和KVO的区别及应用 KVC/KVO原理 1. KVC键值编码 KVC,即是指NSKey...

  • KVC KVO高阶应用

    KVC KVO高阶应用 KVC KVO高阶应用

网友评论

    本文标题:Swift3.0 KVC & KVO

    本文链接:https://www.haomeiwen.com/subject/pmjxyttx.html