I'm adding cache to my app with PINCache and I am in a situation where delegate methods to encode/decode are called by the cache system.
Those methods are generic but the generic value do not conform explicitly to Codable
. Because they are delegates, I can't change the signature to make the generic type conform to Codable
.
func modelForKey<T : SimpleModel>(_ cacheKey: String?, context: Any?, completion: @escaping (T?, NSError?) -> ()) {
guard let cacheKey = cacheKey, let data = cache.object(forKey: cacheKey) as? Data, T.self is Codable else {
completion(nil, nil)
return
}
let decoder = JSONDecoder()
do {
let model: T = try decoder.decode(T.self, from: data)
completion(model, nil)
} catch {
completion(nil, nil)
}
}
With this code, I'm having the following error:
In argument type
T.Type
,T
does not conform to expected typeDecodable
How can I force my decoder
to accept the generic value?
Since Codable can't be implemented in extensions (yet?) and since SimpleModel is internal to PINCache you can't make it conform to Codable.
If possible I would suggest switching to a caching library with a protocol that supports Codable like Cache
Try func modelForKey<T : SimpleModel, Decodable> ...
to require that type is constrained to Decodable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With