为什么不符合相关类型的协议?

问题描述 投票:0回答:1

我有以下简单协议:

protocol JSONParser {

   associatedtype JSONResult
   func parse<T: Codable>(response: Response, type: T) -> JSONResult

}

功能parse取简单的Response作为结构,取TCodable。这是我实现此协议的方式:

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)
}

我无法弄清楚我的实现有什么问题。我想使用通用函数,而不是通用结构。

swift generics swift-protocols protocol-oriented
1个回答
0
投票
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> {

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.