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

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

iamhenry_c_s_d_n 回答:有什么办法可以将数字用作变量的名称?

如果“ 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
    }
}
本文链接:https://www.f2er.com/3161976.html

大家都在问