swift - Decodable 类型的数组不可解码

标签 swift types decodable

如果我们有一个类型 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/

相关文章:

swift - firebase 照片丢失时崩溃

swift - 第一次使用 Xcode 7 时,我收到了一个额外的参数错误调用,代码如下 -

java - 逆变类型上的 Kotlin 星投影

html - 如何在Angular中仅打印错误消息

ios - JSON Decodable - 按键搜索

ios - Xamarin iOS 如何创建不可重用的 UITableViewCells

ios - segue 快速获得 twise 调用

Javascript:如何检查字符串类型?

json - 如何将 JSONDecoder 与多种可能的数据结构一起使用

使用 Decodable 协议(protocol)进行 JSON 解析