【问题标题】:Use "/" and "%" in Codable - Swift [duplicate]在 Codable 中使用“/”和“%” - Swift [重复]
【发布时间】:2018-05-23 20:49:37
【问题描述】:

我使用 API 来解析一些数据 - 这很有效。但是现在 api 使用像“d/a”和“test%”这样的名称,我不能用这个符号创建一个可编码的结构。有没有办法使用它们?

提前致谢

【问题讨论】:

    标签: json swift api struct codable


    【解决方案1】:

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

    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了解更多信息

    请记住,尽管您必须手动包含所有要编码/解码的属性,并且“枚举案例的名称应该与您在你的类型"

    【讨论】:

    • 感谢您的快速答复!祝你有美好的一天?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多