如何在 var 名称中使用 #。 JSON、iOS、Swift

How can I use # in a var name. JSON, iOS, Swift

我正在尝试使用 decodable 来解析一些 json 但 json 中的其中一个名称中有一个 #。

如何将它添加到我的 var 中,如下所示?

"image": [
    { 
        "#text": "https…",
        "size": "small"
    },

你不能。 Swift 变量必须以字母或下划线开头。

可以 做的是添加 CodingKeys 以在 JSON 中的字段名称和对象中的 属性 之间进行转换...

struct Image: Decodable {
    let text: String
    let size: String

    enum CodingKeys: String, CodingKey {
        case text = "#text", size
    }
}

应该做