在 Codable 中使用“/”和“%” - Swift

Use "/" and "%" in Codable - Swift

我使用 API 来解析一些数据 - 这有效。但是现在 api 使用 "d/a" 和 "test%" 之类的名称,我无法使用这些符号创建可编码结构。有没有办法使用它们?

提前致谢

您可以重新映射您的键。这是一个基本示例:

struct MyObject: Codable {
    var name: String
    var da: String
    var test: String

    enum CodingKeys: String, CodingKey {
        case name
        case da = "d/a"
        case test = "test%"
    }
}

您可以在 documentation here

中阅读更多相关信息

请记住,您必须手动包含所有您想要的属性 encoded/decoded 并且“枚举案例的名称应与您指定的名称相匹配赋予您类型中的相应属性"