協議我想使用SWIFT一般如下代碼顯示:可以投噸至comform在迅速
func handle<T>(data: Data, with type: T.Type) {
if type is B.Type {
handleOne(data: data, with: type) //error here: In argument type 'T.Type', 'T' does not conform to expected type 'B'
// cast T comform B?
} else {
handleTwo(data: data)
}
}
func handleOne<T>(data: Data, with type: T.Type) where T:B {
}
func handleTwo(data: Data) {
}
...
protocol B {
...
}
B
是一個協議,我可以打電話handleOne
在handle
?可以鑄造T
通知B
?
編譯器**不**足夠聰明,知道'如果類型是B.Type'是TRUE;然後類型實際上是'B'型。由於'B'類型是'handleOne'函數的一個要求,所以這會失敗! – Honey
我不這麼認爲,我在Xcode9 swift4中試過,使用'struct A:B {}','handle(data:Data(),with:A.self)'',如果type是B.Type'是'true',你可以試試這個 –