Swift 4 可编码和泛型
Swift 4 Codable & generics
我正在使用 PINCache 将缓存添加到我的应用程序,并且我处于缓存系统调用 encode/decode 的委托方法的情况。
这些方法是通用的,但通用值不明确符合 Codable
。因为它们是委托,所以我无法更改签名以使泛型符合 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)
}
}
使用此代码,我遇到以下错误:
In argument type T.Type
, T
does not conform to expected type Decodable
如何强制我的 decoder
接受通用值?
更改此行以检查它是否符合 Decodable
:
guard let cacheKey = ... as? Data, T.self is Decodable else {
由于 Codable 不能在扩展中实现(还?)并且因为 SimpleModel 是 PINCache 的内部,所以你不能让它符合 Codable。
如果可能的话,我建议切换到具有支持 Codable 的协议的缓存库,例如 Cache
尝试func modelForKey<T : SimpleModel, Decodable> ...
要求将类型限制为 Decodable。
IMO 问题不在于 PINCache。
T.self is Codable
没有告诉编译器更多关于类型 T
的信息,所以 decoder.decode(T.self, from: data)
不会通过类型检查,即使 T
是 Decodable
.
我认为分支 RocketData 将是这里最简单的解决方案(如果你想继续使用 RocketData + Decodable
并且你的所有模型都符合 Decodable
)。使 SimpleModel
符合 Decodable
.
尝试创建CustomProtocol: Codable, SimpleModel
如果第 1 点不起作用,请尝试创建自定义 class CustomClass: SimpleModel, Codable
并使用 modelForKey<T : CustomClass>
我正在使用 PINCache 将缓存添加到我的应用程序,并且我处于缓存系统调用 encode/decode 的委托方法的情况。
这些方法是通用的,但通用值不明确符合 Codable
。因为它们是委托,所以我无法更改签名以使泛型符合 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)
}
}
使用此代码,我遇到以下错误:
In argument type
T.Type
,T
does not conform to expected typeDecodable
如何强制我的 decoder
接受通用值?
更改此行以检查它是否符合 Decodable
:
guard let cacheKey = ... as? Data, T.self is Decodable else {
由于 Codable 不能在扩展中实现(还?)并且因为 SimpleModel 是 PINCache 的内部,所以你不能让它符合 Codable。
如果可能的话,我建议切换到具有支持 Codable 的协议的缓存库,例如 Cache
尝试func modelForKey<T : SimpleModel, Decodable> ...
要求将类型限制为 Decodable。
IMO 问题不在于 PINCache。
T.self is Codable
没有告诉编译器更多关于类型 T
的信息,所以 decoder.decode(T.self, from: data)
不会通过类型检查,即使 T
是 Decodable
.
我认为分支 RocketData 将是这里最简单的解决方案(如果你想继续使用 RocketData + Decodable
并且你的所有模型都符合 Decodable
)。使 SimpleModel
符合 Decodable
.
尝试创建
CustomProtocol: Codable, SimpleModel
如果第 1 点不起作用,请尝试创建自定义 class
CustomClass: SimpleModel, Codable
并使用modelForKey<T : CustomClass>