這是我的代碼段:鑄造在斯威夫特工作不正常
if let newValue = change?[NSKeyValueChangeNewKey] {
print("\(newValue)")
faceBounds = newValue as? CGRect
print("\(faceBounds)" + " in controller")
dispatch_async(dispatch_get_main_queue(),{() -> Void in self.updateFaceRectView()})
}
其中「faceBounds」是的CGRect?類型。
然而,這是從我得到的系統輸出:
的NSRect:{{116.24999,337.49997},{86.249992,86.249992}}
在控制器零
發生什麼事?爲什麼不是「faceBounds」獲得正確的價值?
[更新]
想這也
if let newValue = change?[NSKeyValueChangeNewKey] {
var str: String?
str = newValue as? String
print("\(str)")
// Still prints nil
}
是我用錯了鑄造知識?
而btw如果有人想知道,newValue是AnyObject類型。
是您的'newValue'曾經分配? – Superian007
newValue如上所示打印出來 –
'CGRect'和'NSRect'是等價的。這個鑄造在Objective C中正常工作。我沒有在Swift中進行測試。但你可以嘗試從'NSRect'手動創建'CGRect'對象。 – t4nhpt