自定义结构:类型不符合协议“可解码” [英] Custom Struct: Type does not conform to protocol 'Decodable'
问题描述
我希望能够将自定义结构
保存到 UserDefaults
,但是为此,我需要它是 Codable
..我这样尝试过:
I would like to be able to save a Custom-struct
to UserDefaults
but for that I need it to be Codable
.. I tried it like this:
struct Wishlist: Codable {
var name: String
var image: UIImage
var wishData: [Wish]
var color: UIColor
var textColor: UIColor
var index: Int
}
但这给了我这个错误
:
类型愿望清单不符合协议可解码
Type 'Wishlist' does not conform to protocol 'Decodable'
这是我的 Class Wish
,也许就是问题所在:
Here is my Class Wish
, maybe that's where the problem is:
class Wish: NSObject {
public var wishName : String?
public var checkedStatus : Bool?
public var wishLink : String?
public var wishPrice : String?
public var wishNote : String?
public var wishImage : UIImage?
init(withWishName name: String, link: String, price: String, note: String, image: UIImage, checked: Bool) {
super.init()
wishName = name
checkedStatus = checked
wishLink = link
wishPrice = price
wishNote = note
wishImage = image
}
}
我在这里做什么错了??
What am I doing wrong here??
推荐答案
您需要使愿望
采用 Codable
。
但是因为 UIImage
和 UIColor
不是 Codable
,您必须按照编码和解码自定义类型:
But because UIImage
and UIColor
are not Codable
, you’ll have to manually implement them as outlined in Encoding and Decoding Custom Types:
struct Wishlist: Codable {
var name: String
var image: UIImage
var wishes: [Wish]
var color: UIColor
var textColor: UIColor
var index: Int
enum CodingKeys: String, CodingKey {
case name, image, wishData, color, textColor, index
}
init(name: String, image: UIImage, wishes: [Wish], color: UIColor, textColor: UIColor, index: Int) {
self.name = name
self.image = image
self.wishes = wishes
self.color = color
self.textColor = textColor
self.index = index
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
wishes = try values.decode([Wish].self, forKey: .wishData)
color = try values.decode(Color.self, forKey: .color).uiColor
textColor = try values.decode(Color.self, forKey: .textColor).uiColor
index = try values.decode(Int.self, forKey: .index)
let data = try values.decode(Data.self, forKey: .image)
guard let image = UIImage(data: data) else {
throw DecodingError.dataCorruptedError(forKey: .image, in: values, debugDescription: "Invalid image data")
}
self.image = image
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(wishes, forKey: .wishData)
try container.encode(Color(uiColor: color), forKey: .color)
try container.encode(Color(uiColor: textColor), forKey: .textColor)
try container.encode(index, forKey: .index)
try container.encode(image.pngData(), forKey: .image)
}
}
struct Wish: Codable {
public var name: String
public var checkedStatus: Bool
public var link: String
public var price: String
public var note: String
public var image: UIImage
init(name: String, link: String, price: String, note: String, image: UIImage, checkedStatus: Bool) {
self.name = name
self.checkedStatus = checkedStatus
self.link = link
self.price = price
self.note = note
self.image = image
}
enum CodingKeys: String, CodingKey {
case name, checkedStatus, link, price, note, image
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
checkedStatus = try values.decode(Bool.self, forKey: .checkedStatus)
link = try values.decode(String.self, forKey: .link)
price = try values.decode(String.self, forKey: .price)
note = try values.decode(String.self, forKey: .note)
let data = try values.decode(Data.self, forKey: .image)
guard let image = UIImage(data: data) else {
throw DecodingError.dataCorruptedError(forKey: .image, in: values, debugDescription: "Invalid image data")
}
self.image = image
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(checkedStatus, forKey: .checkedStatus)
try container.encode(link, forKey: .link)
try container.encode(price, forKey: .price)
try container.encode(note, forKey: .note)
try container.encode(image.pngData(), forKey: .image)
}
}
我在哪里d将此作为编码 UIColor
对象的便捷方法:
Where I’d use this as a convenient way to encode UIColor
objects:
struct Color: Codable {
let red: CGFloat
let green: CGFloat
let blue: CGFloat
let alpha: CGFloat
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
init(uiColor: UIColor) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
uiColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
var uiColor: UIColor { UIColor(red: red, green: green, blue: blue, alpha: alpha) }
}
注意,我做了几个不相关的更改:
Note, I did a couple of unrelated changes:
-
我都做了这些
结构
。除非必要,否则我不会介绍引用类型(更少的NSObject
子类)。
I made both of these
struct
. I wouldn’t introduce reference types (much lessNSObject
subclasses) unless necessary.
我简化了一些属性名称。例如。在愿望
中,我们通常不会在属性名称中使用 wish
前缀。我也不会在属性名称中使用数据,除非它实际上是 Data
。
I simplified some of the property names. E.g. in Wish
, we wouldn’t generally use wish
prefix in property names. I also wouldn’t use "data" in a property name unless it was, in fact, a Data
.
我更新了 init
方法以使用标准命名约定。
I updated init
methods to use standard naming conventions.
这篇关于自定义结构:类型不符合协议“可解码”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!