这个问题在这里已经有了答案:
How do I make an enum Decodable in swift 4?
(9 个回答)
2年前关闭。
我有一个嵌套的 enum
在 struct
内我想符合Codable
.如何制作枚举 codable
因此使 struct
可编码?
这是我所拥有的一个例子:
struct Person: Codable {
var firstName: String
var lastName: String
var favoriteColor: Color
enum Color {
case blue, red, green, yellow, pink, purple
}
}
然后,我得到两个错误:
Type 'Person' does not conform to protocol 'Decodable'
Type 'Person' does not conform to protocol 'Encodable'
我该如何解决这个问题?
编辑
我也尝试过使颜色符合 Codable。 Xcode 添加了这些协议(protocol) stub :
init(from decoder: Decoder) throws {
<#code#>
}
func encode(to encoder: Encoder) throws {
<#code#>
}
我该怎么办?
最佳答案
struct Person: Codable {
var firstName: String
var lastName: String
var favoriteColor: Color
}
enum Color: String, Codable {
case blue, red, green, yellow, pink, purple
}
关于ios - 如何使枚举可编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60957181/