【问题标题】:error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)错误:执行被中断,原因:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
【发布时间】:2016-08-23 07:22:02
【问题描述】:

我有一个使用 KVO 功能的代码。

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    ...
    let oldRect = change?[NSKeyValueChangeOldKey] as! NSRect

所以当我尝试将值转换为 NSRect 时,我得到了错误:

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

并且对象是NSRect类型的对象:

(lldb) po change?[NSKeyValueChangeOldKey]
▿ Optional<AnyObject>
  - Some : NSRect: {{293, 21}, {108, 108}}

【问题讨论】:

    标签: swift macos casting


    【解决方案1】:

    如您所知,observeValueForKeyPath(_:ofObject:change:context:)change 参数是[String : AnyObject]? 类型,在内部,它是一个NSDictionary,它不能包含NSRange 作为值。

    因此,KVO 将值转换为包含NSRangeNSValue。但不幸的是,它不能直接桥接到 Swift 中的NSRange

    试试这样的:

        let oldRect = (change?[NSKeyValueChangeOldKey] as! NSValue).rectValue
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多