如果我们有一个类型 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)
最佳答案
只需使用这个:
let res = JSONDecoder().decode([Foo].self, data)
对于您的情况:
exampleMethod(类型:[Foo()])
关于exampleMethod
签名,您应该编写一个数组。不是数组类型。
关于swift - Decodable 类型的数组不可解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125291/