i created a propertyWrapper like this:
@propertyWrapper
public struct DefaultTodayDate: Codable {
public var wrappedValue: Date
private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "y-MM-dd'T'HH:mm:ss"
return formatter
}()
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
var stringDate = ""
do {
stringDate = try container.decode(String.self)
self.wrappedValue = self.dateFormatter.date(from: stringDate) ?? Date()
} catch {
self.wrappedValue = Date()
}
}
public func encode(to encoder: Encoder) throws {
try wrappedValue.encode(to: encoder)
}
}
and a model like this:
struct MyModel: Codable {
@DefaultTodayDate var date: Date
}
so, if i want to parse this json file, everything is ok:
let json = #"{ "date": "2022-10-10T09:09:09" }"#.data(using: .utf8)!
let result = try! JSONDecoder().decode(MyModel.self, from: json)
print(result) // result.date is: 2022-10-10 09:09:09 +0000
-----
let json = #"{ "date": "" }"#.data(using: .utf8)!
let result = try! JSONDecoder().decode(MyModel.self, from: json)
print(result) // result.date is: Date()
-----
let json = #"{ "date": null }"#.data(using: .utf8)!
let result = try! JSONDecoder().decode(MyModel.self, from: json)
print(result) // result.date is: Date()
but i want to also parse a json without date
property.but i get. fatal error:
let json = #"{ "book": "test" }"#.data(using: .utf8)!
let result = try! JSONDecoder().decode(MyModel.self, from: json)
// Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "date", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"date\", intValue: nil) (\"date\").", underlyingError: nil))
print(result) // i want to result.date be Date()