【发布时间】:2018-01-25 04:09:51
【问题描述】:
我在玩 Swift 4 和 Codable 时遇到了一些场景,其中的嵌套协议都符合 Codable。
简化示例如下所示:
protocol CodableSomething: Codable {}
protocol CodableAnotherThing: Codable {
var something: CodableSomething { get }
}
struct Model: CodableAnotherThing {
var something: CodableSomething
}
This code 在 Xcode 9 Beta 5 中出现构建错误:
- 类型“模型”不符合协议“可解码”
- 类型“模型”不符合协议“可编码”
现在,我没想到会出现这些错误,因为我知道编译器会自动生成对这些协议的一致性,而事实上,如果没有构建错误,我什至无法手动实现这种一致性。我还尝试了几种不同的方法来使用Codable 来解决这种嵌套模型结构,但我就是无法让它工作。
我的问题:这是编译器错误(仍是测试版)还是我做错了什么?
【问题讨论】: