如何检查Swift中字典中的NSObject项?

时间:2015-09-01 09:16:48

标签: ios swift

self.tabBar.addObserver(self, forKeyPath: "hidden", options: NSKeyValueObservingOptions.New, context:tabBarContext)



override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    println(change)
    if context == tabBarContext && keyPath == "hidden"{
        //how to check if change[new] is 1 or 0
    }
}

当我打印change时,我得到:

[kind: 1, new: 0]

我希望能够检查&#34; new&#34;是0还是1.如何正确地进行类型转换?

2 个答案:

答案 0 :(得分:0)

使用KVO NSKeyValueChangeNewKey 提供的常量字符串。

let newChange = change[NSKeyValueChangeNewKey]

答案 1 :(得分:0)

尝试展开

if let dictChange: Dictionary<String, Int>  = change as? Dictionary<String, Int> {
      let isNew = dictChange["new"]
}