におけるプロトコルcomformするTをキャストすることができます:は、私は以下のコード・ショーとして迅速ジェネリックを使用したい迅速
protocol B {
...
}
B
がプロトコルである...
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) {
}
を、私はhandle
でhandleOne
を呼び出すことができます?キャスト可能T
comform B
?
コンパイラ**はタイプがB.Type'であれば '知るには十分**スマートではありませんtrue'には、次に入力し'です実際にはタイプBである。あなたの 'handleOne'関数には' B'型が必要なので、これは失敗します! – Honey
私はXcode9 swift4で試してみましたが、 'struct A:B {}'、 'handle(data:Data()、with:A.self) '、'型がB.Typeならば'true'、あなたはこれを試すことができます –