Swift 4.0 Decoder error with closure property

829 views Asked by At

I'm trying to serialize my objects with the Swift 4.0 Codable protocol. I'm hitting an error when trying to decode closure properties:

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

Understandable enough, I suppose, but surely there must be some strategy I can use (after all, functions are first-class objects, right?). Do I have to wrap my closures somehow?

1

There are 1 answers

0
Rohit Sisodia On

You can use a trick to solve that:

typealias YourCompletion = (_ status: Bool) -> Void
class Completion: Codable {
    var completion: YourCompletion?
    private enum CodingKeys: CodingKey {}
}