【发布时间】:2017-10-04 18:16:00
【问题描述】:
从后端接收消息,现在我想使用 swift 4 JSON 解码。我使用具有常规属性(String、Int、...)的对象对其进行了测试,并且工作没有问题。现在我想处理具有枚举属性的对象,我想在对象初始化时设置该属性。如何设置初始化,使其包括设置枚举?
struct WebPacket: Decodable {
let type: MessageType
let message: String
init(with type: MessageType, data: Data) {
type = type
// fill others
}
}
enum MessageType: String {
case unknown
case getDescription = "get-daa-description"
case description = "daa-description"
case holdings = "holdings"
}
【问题讨论】:
-
对不起,我修好了。这个初始化函数只是我想要实现的即兴创作。