【发布时间】:2019-07-24 18:24:02
【问题描述】:
我想在 Swift 中编码和解码 CGSize 数据类型并将其存储在字典中,但我找不到任何示例代码如何做到这一点。我试过了,但它给出了错误:
let size = CGSize(width: Int(100), height: Int(50))
dictionary["size"] = try! size.encode(to: Encoder())
Error: 'Encoder' cannot be constructed because it has no accessible initializers
我理解 Encoder 是一个协议,我们应该使用哪个 Encoder/Decoder 类?
【问题讨论】:
-
是否有理由在将
size存储到dictionary之前对其进行编码?如果dictionary类型为[String: CGSize],则可以直接dictionary["size"] = size。 -
嗯,CGSize 确认到 Codable/plist 了吗?因为我还需要将此字典存储为 plist 并可选择将其发送到服务器。
标签: ios swift swift4 codable decodable