FYI:雨燕的錯誤提出這裏:https://bugs.swift.org/browse/SR-3871未能從任何Swift中投射?以協議
我在哪裏鑄造不工作的奇怪的問題,但控制檯顯示它爲正確的類型。
我有一個公共的協議
public protocol MyProtocol { }
我實現這個模塊中,與返回實例的公共方法。
internal struct MyStruct: MyProtocol { }
public func make() -> MyProtocol { return MyStruct() }
然後,在我的視圖控制器,我觸發與該對象賽格瑞作爲發件人
let myStruct = make()
self.performSegue(withIdentifier: "Bob", sender: myStruct)
所有好爲止。
問題出在我的prepare(for:sender:)
方法中。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Bob" {
if let instance = sender as? MyProtocol {
print("Yay")
}
}
}
但是,實例到MyProtocol的轉換總是返回nil
。
當我在控制檯中運行po sender as! MyProtocol
時,它給了我錯誤Could not cast value of type '_SwiftValue' (0x1107c4c70) to 'MyProtocol' (0x1107c51c8)
。但是,po sender
將輸出有效的Module.MyStruct
實例。
爲什麼不能投射?
(我已經成功通過拳擊我的協議的結構來解決這個問題,但我想知道爲什麼它不工作的是,如果有更好的方法來解決它)
只是在這裏出門,但在這裏更改內部聲明'內部結構MyStruct:MyProtocol {}'到'公共'改變什麼? – Dennis
@ Dennis Nope :( – deanWombourne