热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在关联枚举中使用Codable

场景:在同一个接口中服务器根据不同的status返回不同的json格式。服务器返回的json如下:1-userisconfirmed:{status

场景:在同一个接口中服务器根据不同的status返回不同的json格式。

  1. 服务器返回的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."
}

  1. 如何构造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
}

  1. 实现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


转载于:https://juejin.im/post/5ceba33ef265da1b8466c35c


推荐阅读
author-avatar
我不是咸鱼仔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有