Swift 我可以为CodingKey设置两个字符串值吗?
比如:Swift 我可以为CodingKey设置两个字符串值吗?,swift,encoding,Swift,Encoding,比如: enum CodingKeys: String, CodingKey { case id = "id" = "some_id" case name case tags case text } 我需要这个,因为我的实体在后端的结构除了id没有之外,还有相同的字段,但是你可以在后台做一些类似这样的“黑客”操作: struct BusinessObject: Codable { enum CodingKeys: String, CodingK
enum CodingKeys: String, CodingKey {
case id = "id" = "some_id"
case name
case tags
case text
}
我需要这个,因为我的实体在后端的结构除了
id
没有之外,还有相同的字段,但是你可以在后台做一些类似这样的“黑客”操作:
struct BusinessObject: Codable {
enum CodingKeys: String, CodingKey {
case primaryId = "id"
case alternativeId = "some_id"
}
private let primaryId: Int?
private let alternativeId: Int?
var id: Int {
return primaryId ?? alternativeId ?? 0
}
}