I have found this extension to use Codable to be saved into NSUserDefaults
extension UserDefaults {
func decode<T : Codable>(for type : T.Type, using key : String) -> T? {
let defaults = UserDefaults.standard
guard let data = defaults.object(forKey: key) as? Data else {return nil}
let decodedObject = try? PropertyListDecoder().decode(type, from: data)
return decodedObject
}
func encode<T : Codable>(for type : T, using key : String) {
let defaults = UserDefaults.standard
let encodedData = try? PropertyListEncoder().encode(type)
defaults.set(encodedData, forKey: key)
defaults.synchronize()
}
}
But as I see I have an error Type 'OfflineRequest' does not conform to protocol 'Decodable'
looks like because of Any
.
I have next structure I want to save:
struct OfflineRequest: Codable {
let url: String
let params: [String: Any]
}
The idea is to persistence store stack (array) of requests which are unsuccessful because of any connection issues. So I have Core Data data model and I am converting its properties to [String: Any] before sending it to the server. But now I want to create offline first algorithm. So in case user is offline I want to persistent store url and params which is [String: Any]. How to handle does not conform to protocol 'Decodable'
correctly in this case?