从KVO获取价值 - 返回NSConreteValue

时间:2016-01-08 21:42:59

标签: swift key-value-observing

我有以下KVO观察员:

var resizable: JQueryUI.Resizable = $('selector').resizable({ ... });

打印出来:

 override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if context == &kvoContext {
            if (keyPath == "transform") {
                if let transform = change![NSKeyValueChangeNewKey] {
                    print("transform: \(transform)")
                    overlay.transform = transform as! CGAffineTransform
                }
            }
        }
    }

但是,在我的变量窗口中,我将transform: CGAffineTransform: {{1, 0, 0, 1}, {0, 0}} 视为NSConcreteValue:

enter image description here

我在最后一行尝试设置transform时崩溃了:

overlay.transform

如何检索该值?

1 个答案:

答案 0 :(得分:1)

您的转换会自动装入NSValue对象。您需要像

一样访问它
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
  if context == &kvoContext {
    if (keyPath == "transform") {
      if let transform = change?[NSKeyValueChangeNewKey] as? NSValue {
        overlay.transform = transform.CGAffineTransformValue()
      }
    }
  }
}