為什么錯誤協議是可能的?對于任何其他協議,我們收到錯誤
protocol SomeProtocol {
}
struct Test1<T> where T: SomeProtocol {
}
struct Test2<T> where T: Error {
}
let test1: Test1<SomeProtocol> = Test1() ----> Protocol 'SomeProtocol' as a type cannot conform to the protocol itself
let test2: Test2<Error> = Test2() ----> It's Ok
uj5u.com熱心網友回復:
這只是規則中的一個例外。它記錄在這里:
例外
當用作型別時,Swift 協議
Error符合自身;@objc沒有靜態要求的協議也可以用作符合自身的型別。
如果不是因為這個例外,你就不能做Result<T, Error>或AnyPublisher<T, Error>說“一些可能是任何錯誤的結果”或“一個可以發出任何錯誤的發布者”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/366531.html
