无法分配“[Currency]”类型的值?输入“[Currency]?.Type”
Cannot assign value of type '[Currency]?' to type '[Currency]?.Type'
我正在尝试解码具有可选数组的结构
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]?
==> Expected member name or
constructor call after type name
编译错误2:
self.Currencies = try productValues.decodeIfPresent([Currency].self,forKey:.Currencies)
==>
Cannot assign value of type '[Currency]?' to type '[Currency]?.Type'
不确定这里缺少什么,有人可以指出吗?
这样声明
var Currencies :[Currency]?
我正在尝试解码具有可选数组的结构
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]?
==> Expected member name or constructor call after type name
编译错误2:
self.Currencies = try productValues.decodeIfPresent([Currency].self,forKey:.Currencies)
==> Cannot assign value of type '[Currency]?' to type '[Currency]?.Type'
不确定这里缺少什么,有人可以指出吗?
这样声明
var Currencies :[Currency]?