我已经为slug
属性创建了一个枚举,使用这个枚举我可以显示按钮上的图标。但是,我无法解码它。它没有给予错误。slug为零。
为什么PlatformModel
中的slug
属性设置为nil
?
型号:
struct PlatformsModel: Codable {
let platform: PlatformModel?
let requirements: RequirementsModel?
}
struct PlatformModel: Codable {
let id: Int?
let name: String?
let slug: PlatformIconType?
}
枚举:
enum PlatformIconType: String, Codable {
case pc = "pc"
case ps3 = "playstation3"
case ps4 = "playstation4"
case ps5 = "playstation5"
case xbox360 = "xbox360"
case xboxSX = "xbox-series-x"
case xboxOne = "xbox-one"
var icon: UIImage {
switch self {
case .pc:
return UIImage(systemName: "desktopcomputer")!
case .ps3:
return UIImage(systemName: "playstation.logo")!
case .ps4:
return UIImage(systemName: "playstation.logo")!
case .ps5:
return UIImage(systemName: "playstation.logo")!
case .xbox360:
return UIImage(systemName: "xbox.logo")!
case .xboxSX:
return UIImage(systemName: "xbox.logo")!
case .xboxOne:
return UIImage(systemName: "xbox.logo")!
}
}
}
JSON格式:
"platforms": [
{
"platform": {
"id": 4,
"name": "PC",
"slug": "pc"
}
},
{
"platform": {
"id": 186,
"name": "Xbox Series S/X",
"slug": "xbox-series-x"
}
},
{
"platform": {
"id": 18,
"name": "PlayStation 4",
"slug": "playstation4"
}
},
{
"platform": {
"id": 16,
"name": "PlayStation 3",
"slug": "playstation3"
}
},
{
"platform": {
"id": 14,
"name": "Xbox 360",
"slug": "xbox360"
}
},
{
"platform": {
"id": 1,
"name": "Xbox One",
"slug": "xbox-one"
}
},
{
"platform": {
"id": 187,
"name": "PlayStation 5",
"slug": "playstation5"
}
}
]
1条答案
按热度按时间dwbf0jvd1#
我相信你的建模有一点不对。这里是一个工作的操场。我做的关键改变是修复你的json的嵌套。在你的例子中有一个未计算的数组。