-1

我正在尝试解码具有可选数组的结构

struct AppInitData:Decodable{

    var ApplicationID:String?
    var Currencies = [Currency]?

    enum CodingKeys: String, CodingKey {
        case ApplicationID = "ApplicationID"
        case Currencies = "Currencies"
    }

    enum DataKeys: String, CodingKey {
        case AppInitData = "Data"
    }

    public init(from decoder: Decoder) throws {

        let values = try decoder.container(keyedBy: DataKeys.self)
        let productValues = try values.nestedContainer(keyedBy: CodingKeys.self, forKey: .AppInitData)

        self.Currencies = try productValues.decodeIfPresent([Currency].self,forKey:.Currencies)

    }

编译错误1:

var Currencies = [Currency]? ==> 类型名称后的预期成员名称或构造函数调用

编译错误2:

self.Currencies = try productValues.decodeIfPresent([Currency].self,forKey:.Currencies)==> 无法分配类型“[货币]?”的值 输入“[货币]?.Type”

不知道这里缺少什么,有人可以指出吗?

4

1 回答 1

1

像这样声明

   var Currencies :[Currency]?
于 2018-02-19T13:45:44.883 回答