2016-08-23 200 views
0

我有一個使用KVO功能的代碼。錯誤:執行中斷,原因:EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)

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

回答

1

如你所知, changeobserveValueForKeyPath(_:ofObject:change:context:)的參數類型爲[String : AnyObject]?,內部爲NSDictionary,它不能包含NSRange作爲值。

因此,KVO將該值轉換爲包含NSRangeNSValue。但不幸的是,它不能直接橋接到Swift中的NSRange

嘗試這樣:

let oldRect = (change?[NSKeyValueChangeOldKey] as! NSValue).rectValue 
相關問題