场景:在同一个接口中服务器根据不同的status返回不同的json格式。
- 服务器返回的json如下:
//1 - user is confirmed:
{"status": "confirmed","confirmedUsers": [{"id": "abc", "name": "Rachel"},{"id": "def", "name": "John"}]
}//2 - user is in waitlist:
{"status": "waitlist","position": 12,"confirmedUsers": [{"id": "abc", "name": "Rachel"},{"id": "def", "name": "John"}]
}//3 - user cannot go for a different reason
{"status": "not allowed","reason": "It is too late to confirm to this event."
}
- 如何构造model?
- 不好的做法:
struct EventConfirmationResponse {let status: Stringlet confirmedUsers: [User]?let position: Int?let reason: String?
}
- 比较好的做法:
enum EventConfirmationResponse {case confirmed([User]) //Contains an array of users going to the eventcase waitlist(Int, [User]) //Contains the position in the waitlist andcase notAllowed(String) //Contains the reason why the user is not allowed
}
- 实现Codable
//declare which keys in the JSON we are interested in
enum CodingKeys: String, CodingKey {case statuscase confirmedUserscase positioncase reason
}//declare the possible values os the status key
private enum EventConfirmationStatus: String, Codable {case confirmedcase waitlistcase notAllowed = "not allowed"
}extension EventConfirmationResponse: Encodable {func encode(to encoder: Encoder) throws {//access the keyed containervar container = encoder.container(keyedBy: CodingKeys.self)//iterate over self and encode (1) the status and (2) the associated value(s)switch self {case .confirmed(let users):try container.encode(EventConfirmationStatus.confirmed, forKey: .status)try container.encode(users, forKey: .confirmedUsers)case .waitlist(let position, let users):try container.encode(EventConfirmationStatus.waitlist, forKey: .status)try container.encode(users, forKey: .confirmedUsers)try container.encode(position, forKey: .position)case .notAllowed(let reason):try container.encode(EventConfirmationStatus.notAllowed, forKey: .status)try container.encode(reason, forKey: .reason)}}
}extension EventConfirmationResponse: Decodable {init(from decoder: Decoder) throws {//access the keyed containerlet container = try decoder.container(keyedBy: CodingKeys.self)//decode the value for the status key into the EventConfirmationStatus enumlet status = try container.decode(EventConfirmationStatus.self, forKey: .status)//iterate over the received status, and try to decode the other relevant valuesswitch status {case .confirmed:let users = try container.decode([User].self, forKey: .confirmedUsers)self = .confirmed(users)case .waitlist:let users = try container.decode([User].self, forKey: .confirmedUsers)let position = try container.decode(Int.self, forKey: .position)self = .waitlist(position, users)case .notAllowed:let reason = try container.decode(String.self, forKey: .reason)self = .notAllowed(reason)}}
}
参考资料:
Using Codable to make enums with associated values even more powerful