reactivecocoa观察全局变量

时间:2018-03-29 14:42:47

标签: swift reactive-cocoa

我已将globalUser定义为A.swift文件中的全局变量。如何在B.swift中观察globalUser的变化。

// A.swift
var globalUser: Dictionary<String, Any?>? = nil


// B.swift
class MeViewController: UITableViewController {
    var user: Dictionary<String, Any?>?

    override func viewDidLoad() {
        super.viewDidLoad()
        // how to bind user with globalUser using ReactiveCocoa? Or Observing globalUser's changing, to update user
    }

}

````

1 个答案:

答案 0 :(得分:0)

将globalUser包装在MutableProperty中并观察:

// A.swift
let globalUser: MutableProperty<[String: Any]?> = MutableProperty(nil)

// B.swift
class MeViewController: UITableViewController {
    var user: [String, Any]?

    override func viewDidLoad() {
        super.viewDidLoad()
        reactive[\.user] <~ globalUser
    }
}