【发布时间】:2020-02-10 01:28:28
【问题描述】:
我有一个名为 Info 的结构,它根据接收到的数据进行解码。但有时,数据中的一个值可以是双精度数或双精度数组。我该如何设置我的结构?
struct Info: Decodable {
let author: String
let title: String
let tags: [Tags]
let price: [Double]
enum Tags: String, Decodable {
case nonfiction
case biography
case fiction
}
}
根据网址,我要么得到双倍价格
{
"author" : "Mark A",
"title" : "The Great Deman",
"tags" : [
"nonfiction",
"biography"
],
"price" : "242"
}
或者我把它当作一个双精度数组
{
"author" : "Mark A",
"title" : "The Great Deman",
"tags" : [
"nonfiction",
"biography"
],
"price" : [
"242",
"299",
"335"
]
}
我想设置我的结构,以便如果我收到一个双精度而不是双精度数组,价格应该被解码为一个 1 双精度数组。
【问题讨论】:
-
你的意思是字符串或字符串数组检查重复项
标签: swift swift4 codable decodable