我正在尝试使用 Swift 4.0 Codable 协议(protocol)序列化我的对象。我在尝试解码闭包属性时遇到错误:
guard let influenceFunction = try? container.decode(((E, Double) -> (E))!.self, forKey: TransformCodingKeys.influenceFunction) else {
// ... do something clever ...
print("sad times...")
}
Cannot invoke 'decode' with an argument list of type '(((E, Double) -> (E))!.Type, forKey: TransformCodingKeys)'
我想这是可以理解的,但肯定有一些我可以使用的策略(毕竟,函数是一流的对象,对吧?)。我必须以某种方式包装我的闭包吗?
最佳答案
你可以使用一个技巧来解决这个问题:
typealias YourCompletion = (_ status: Bool) -> Void
class Completion: Codable {
var completion: YourCompletion?
private enum CodingKeys: CodingKey {}
}
关于Swift 4.0 解码器错误与闭包属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45970903/