请看一下我的代码:
没有错误
struct Person: Codable {
var name: String
var age: Double
var birthday: Date
var selectedItem: Car
}
struct Car: Codable {
var companyName: String
var creationDate: Date
}
struct iPhone: Codable {
var model: String
var creationDate: Date
var isScreenBroken: Bool = true
}
构建错误
struct Person: Codable { // "Type 'Person' does not conform to protocol 'Decodable'", "Type 'Person' does not conform to protocol 'Encodable'"
var name: String
var age: Double
var birthday: Date
var selectedItem: Codable // I've changed this line
}
struct Car: Codable {
var companyName: String
var creationDate: Date
}
struct iPhone: Codable {
var model: String
var creationDate: Date
var isScreenBroken: Bool = true
}
类型“人”不符合协议“可解码”
类型“人”不符合协议“可编码”
我不明白为什么会这样。它知道selectedItem
符合Encodable
& Decodable
:
var selectedItem: Codable
我是 Swift 协议的新手,所以请在回答时尝试解释这里发生了什么。
谢谢!