Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminbenarieb/f32477f1b9ece3d6ec780f60778c2987 to your computer and use it in GitHub Desktop.
Save aminbenarieb/f32477f1b9ece3d6ec780f60778c2987 to your computer and use it in GitHub Desktop.
class ArchiverContainer<T: Codable>: NSObject, NSCoding {
enum ArchiverContainerError: Error {
case failedDecoding
}
let info: T
init(info: T) {
self.info = info
}
required init?(coder: NSCoder) {
do {
guard let data = coder.decodeData() else {
throw ArchiverContainerError.failedDecoding
}
let json: [AnyHashable: Any] = try data.toJSON()
self.info = try T.create(from: json)
}
catch {
os_log(.debug, "Error: %@", error.localizedDescription)
return nil
}
super.init()
}
func encode(with coder: NSCoder) {
guard let data = self.info.toJSONData() else {
return
}
coder.encode(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment