我正在使用Thoughtbot的Argo框架将JSON对象解析为模型。在Argo中解码泛型类型
我遇到了一个问题,我有一个协议及其扩展,像这样
protocol SomeProtocol {
associatedtype Model
func foo()
}
extension SomeProtocol where Model: Decodable {
func foo() -> Model? {
// someJSON is AnyObject in this case, say, from a network call
guard let model: Model = decode(someJSON) else { return nil }
return model
}
}
,并符合本协议类看起来是这样的
class SomeClass: SomeProtocol {
typealias Model = ArgoModel
func bar() {
print(foo())
}
}
和模型像这样
struct ArgoModel {
let id: String
}
extension ArgoModel: Decodable {
static func decode(j: AnyObject) -> Decoded<ArgoModel> {
return curry(self.init)
<^> j <| "id"
}
}
(我也在用他们的咖喱库以及咖喱ini t方法)
我遇到的问题是,在SomeProtocol扩展中,Argo无法解码associatedtype,Model。我得到的错误是
No 'decode' candidates produced the expected contextual result type 'Self.Model?'
这是Swift类型系统的限制吗?还是有我失踪的东西?