我有以下简单协议:
protocol JSONParser {
associatedtype JSONResult
func parse<T: Codable>(response: Response, type: T) -> JSONResult
}
功能parse
取简单的Response
作为结构,取T
的Codable
。这是我实现此协议的方式:
struct AuthJSONParser: JSONParser {
func parse<T: Codable>(response: Response, type: T) -> AuthResult<T> {
}
}
似乎一切正常。但是Xcode表示它不符合协议。这里AuthResult<T>
仅用于情况:
enum AuthResult<Model: Codable> {
case success(data: Model)
case failed(msg: String)
}
我无法弄清楚我的实现有什么问题。我想使用通用函数,而不是通用结构。
protocol JSONParser {
func parse<T: Codable>(response: Response, type: T) -> Result<T, Error>
}
struct AuthJSONParser: JSONParser {
func parse<T: Codable>(response: Response, type: T) -> Result<T, Error> {
}
}
或直接使用AuthResult代替关联的类型
protocol JSONParser {
func parse<T: Codable>(response: Response, type: T) -> AuthResult<T>
}
struct AuthJSONParser: JSONParser {
func parse<T: Codable>(response: Response, type: T) -> AuthResult<T> {
}
}