The structure stores the date value in the Date? format, but after saving it returns it in the timestamp format.
if let data = try? JSONEncoder().encode(contact),
let dict = try? JSONSerialization.jsonObject(with: data, options: .Element.fragmentsAllowed) {
defaults.set(
dict,
forKey: "contact")
}
print(defaults.object(forKey: "contact"))
}
Please tell me how can I save the date in Date format. In this case, it is necessary to preserve the entire structure.
As allready pointed out JSON does not support the
Datetype. It will encode it to an integer by default.But it seems you want to decode it back and use it again as
Date. This is possible by using theJSONDecoder().and you got the same object back that you encoded.
And by the way you don´t need to use
JSONSerializationto store your object inUserDefaultsyou can store the data directly.and retrieving the data:
and decoding it like i´ve shown above.