如果我们有一个类型的Foo: Decodable
,我们如何使Array<Foo>
可解码?
我需要创建另一种类型的Foos: Decodable
吗?
如果是这样的话,如何运作呢?
我在这里看到了问题
func exampleMethod<T:Decodable>(type: T) { }
// Argument type 'Array<Foo>.Type' does not conform to expected type 'Decodable'
exampleMethod(type: [Foo].self)
发布于 2019-09-26 22:04:58
就用这个吧:
let res = JSONDecoder().decode([Foo].self, data)
就你的情况而言:
exampleMethod(type: [Foo()])
关于exampleMethod
签名,您应该编写一个数组。不是数组的类型。
发布于 2019-09-26 22:05:08
如果Foo
符合Decodable
,那么Array<Foo>
应该自动遵守Decodable
。这不是你在这个案子里看到的吗?
https://stackoverflow.com/questions/58125291
复制相似问题