本文介绍了从KVO获取价值-返回NSConreteValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下KVO观察者:

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

哪个打印出来:

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

但是,在变量窗口中,我看到transform作为NSConcreteValue:

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

然后我在尝试设置overlay.transform的最后一行崩溃了:

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

fatal error: unexpectedly found nil while unwrapping an Optional value

如何获取该值?

推荐答案

您的变换将自动装箱到NSValue对象中.您需要像访问它一样

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

这篇关于从KVO获取价值-返回NSConreteValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 10:50