有什么方法可以将数字用作变量的名称?

问题描述 投票:0回答:1

我有一个JSON响应,其中JSON的键值为“ 0”。我正在使用Swift的Codable协议来解析我的JSON。有什么办法可以将“ 0”作为变量名或其他解决方法?我了解用于解析JSON的SwiftyJSON,但我更喜欢使用本地swift协议。This is my JSON response.

json ios13 swift4.2
1个回答
0
投票

如果“ 0”键是常量,则可以使用CodingKeys将其映射到有效的属性名称,如下所示:

struct Response: Codable {
    let success: Bool
    let data: DataClass
    let error, message: String
}

struct ZeroKeyType: Codable {
    // properties go here
}

struct DataClass: Codable {
    let the0: ZeroKeyType
    let searchField: [String]

    enum CodingKeys: String, CodingKey {
        case the0 = "0"
        case searchField
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.