public class Hoge: Codable { public var id: Int public var name: String // これ追加 let creationDate: Date
public enum CodingKeys: String, CodingKey, CaseIterable {
case id
}
// これ追加
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
name = try container.decode(String?.self, forKey: .name)
// ここ注意
creationDate = try container.decode(Date.self, forKey: .creationDate)
}
}
String?.self
ができる
extension Hoge: Hashable { public static func == (lhs: Hoge, rhs: Hoge) -> Bool { return lhs.id == rhs.id && lhs.creationDate == rhs.creationDate } public func hash(into hasher: inout Hasher) { hasher.combine(id) hasher.combine(creationDate) } }