Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4 Codable & generics

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 type Decodable

How can I force my decoder to accept the generic value?

like image 244
Morniak Avatar asked Oct 12 '17 13:10

Morniak


2 Answers

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

like image 56
ahbou Avatar answered Nov 04 '22 02:11

ahbou


Try func modelForKey<T : SimpleModel, Decodable> ... to require that type is constrained to Decodable.

like image 38
Oliver Langan Avatar answered Nov 04 '22 02:11

Oliver Langan