2

我正在尝试使用 Swift 4.0 Codable 协议序列化我的对象。尝试解码闭包属性时遇到错误:

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)'

我想这是可以理解的,但肯定有一些我可以使用的策略(毕竟,函数是一流的对象,对吧?)。我必须以某种方式包装我的闭包吗?

4

1 回答 1

1

你可以使用一个技巧来解决这个问题:

typealias YourCompletion = (_ status: Bool) -> Void
class Completion: Codable {
    var completion: YourCompletion?
    private enum CodingKeys: CodingKey {}
}
于 2019-08-08T12:56:56.653 回答