Get value from KVO - returning NSConreteValue

不羁岁月 提交于 2019-12-11 12:55:19

问题


I have the following KVO observer:

 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
                }
            }
        }
    }

Which prints out:

transform: CGAffineTransform: {{1, 0, 0, 1}, {0, 0}}

However, in my variable window I see transform as an NSConcreteValue:

And I crash on that last line trying to set overlay.transform:

fatal error: unexpectedly found nil while unwrapping an Optional value

How do I retrieve that value?


回答1:


Your transform is being automatically boxed into an NSValue object. You'll need to access it like

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()
      }
    }
  }
}


来源:https://stackoverflow.com/questions/34686618/get-value-from-kvo-returning-nsconretevalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!