swift 无法解码字符串枚举

kkbh8khc  于 2022-12-17  发布在  Swift
关注(0)|答案(1)|浏览(168)

我已经为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"
                }
            }
        ]
dwbf0jvd

dwbf0jvd1#

我相信你的建模有一点不对。这里是一个工作的操场。我做的关键改变是修复你的json的嵌套。在你的例子中有一个未计算的数组。

import UIKit
import Foundation

struct PlatformModel: Codable {
    let id: Int?
    let name: String?
    let slug: PlatformIconType?
}

struct PlatformsModel: Codable {
    let platform: PlatformModel?
}

struct Platforms: Codable {
    let platforms: [PlatformsModel]
}

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")!
        }
    }
}

let 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"
                }
            }
        ]
}
"""

let jsonData = json.data(using: .utf8)!
let model = try! JSONDecoder().decode(Platforms.self, from: jsonData)

print(model.platforms.first?.platform?.slug?.rawValue) //Optional("pc")

相关问题