响应结构不喜欢 CodingKeys
Response struct does not like CodingKeys
我有这个结构用于解码来自服务器的 JSON:
struct AdminResponse: Codable {
let Status: Int?
let SuperUsers: [SuperUser]?
let Message: String?
}
struct SuperUser: Codable {
let name:String?
let id:String?
}
这非常有效。我能够解码 JSON.
但是我不喜欢这些首字母大写的属性,所以我喜欢
struct AdminResponse: Codable {
let Status: Int?
let SuperUsers: [SuperUser]?
let Message: String?
enum CodingKeys: String, CodingKey {
case status = "Status"
case superUsers = "SuperUsers"
case message = "Message"
}
}
struct SuperUser: Codable {
let name:String?
let id:String?
enum CodingKeys: String, CodingKey {
case name = "name"
case id = "id"
}
}
Error: Type AdminResponse does not conforme to protocol Decodable.
为什么???
来自文档 Encoding and Decoding Custom Types:
Codable types can declare a special nested enumeration named
CodingKeys that conforms to the CodingKey protocol. When this
enumeration is present, its cases serve as the authoritative list of
properties that must be included when instances of a codable type are
encoded or decoded. The names of the enumeration cases should match
the names you've given to the corresponding properties in your type.
换句话说:
struct CodableStruct: Codable {
let myPropertyName: SomeCodableType
enum CodingKeys: String, CodingKey {
case myPropertyName = "WhateverIsMatchingInRealityTheJSON"
}
}
并且 myPropertyName
必须是 CodableStruct
的 var 的名称以及 CodingKeys
的 case
的名称。
在你的情况下,由于建议以小写字母开头命名 var,我会这样做:
struct AdminResponse: Codable {
let status: Int?
let superUsers: [SuperUser]?
let message: String?
enum CodingKeys: String, CodingKey {
case status = "Status"
case superUsers = "SuperUsers"
case message = "Message"
}
}
我有这个结构用于解码来自服务器的 JSON:
struct AdminResponse: Codable {
let Status: Int?
let SuperUsers: [SuperUser]?
let Message: String?
}
struct SuperUser: Codable {
let name:String?
let id:String?
}
这非常有效。我能够解码 JSON.
但是我不喜欢这些首字母大写的属性,所以我喜欢
struct AdminResponse: Codable {
let Status: Int?
let SuperUsers: [SuperUser]?
let Message: String?
enum CodingKeys: String, CodingKey {
case status = "Status"
case superUsers = "SuperUsers"
case message = "Message"
}
}
struct SuperUser: Codable {
let name:String?
let id:String?
enum CodingKeys: String, CodingKey {
case name = "name"
case id = "id"
}
}
Error: Type AdminResponse does not conforme to protocol Decodable.
为什么???
来自文档 Encoding and Decoding Custom Types:
Codable types can declare a special nested enumeration named CodingKeys that conforms to the CodingKey protocol. When this enumeration is present, its cases serve as the authoritative list of properties that must be included when instances of a codable type are encoded or decoded. The names of the enumeration cases should match the names you've given to the corresponding properties in your type.
换句话说:
struct CodableStruct: Codable {
let myPropertyName: SomeCodableType
enum CodingKeys: String, CodingKey {
case myPropertyName = "WhateverIsMatchingInRealityTheJSON"
}
}
并且 myPropertyName
必须是 CodableStruct
的 var 的名称以及 CodingKeys
的 case
的名称。
在你的情况下,由于建议以小写字母开头命名 var,我会这样做:
struct AdminResponse: Codable {
let status: Int?
let superUsers: [SuperUser]?
let message: String?
enum CodingKeys: String, CodingKey {
case status = "Status"
case superUsers = "SuperUsers"
case message = "Message"
}
}