【发布时间】:2020-02-13 13:51:03
【问题描述】:
我有这个结构
struct Photo: Codable {
var image: UIImage
let caption: String?
let location: CLLocationCoordinate2D?
}
private enum CodingKeys: String, CodingKey {
case image = "image"
case caption = "caption"
case location = "location"
}
我收到这 2 个错误:
类型“照片”不符合协议“可解码”
类型“照片”不符合协议“可编码”
【问题讨论】:
-
UIImage应该是Codable,但它不是。CLLocationCoordinate2D也必须是Codable。另外CodingKeys必须是Photo的内部成员。在这种情况下,根本不需要它们。您最好的选择是通过转换为Data(png/jpeg) 并可能转换为String对UIImage进行自定义编码/解码。