For some reason, I need to serialize a class object that contains several closure fields.
Like this:
import Foundation
class Foo: NSObject, NSCoding {
var bar: (() -> Void)
override init() {
bar = {}
}
required init(coder aDecoder: NSCoder) {
bar = aDecoder.decodeObject(forKey: "bar") as! (() -> Void)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(bar, forKey: "bar")
}
}
let foo = Foo()
foo.bar = {
print("Help!")
}
let data = NSKeyedArchiver.archivedData(withRootObject: foo)
let object = NSKeyedUnarchiver.unarchiveObject(with: data) as! Foo
Is there any way I can get what I want?