Skip to content

Instantly share code, notes, and snippets.

@danielgarbien
Created October 29, 2019 06:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgarbien/849467304a114fb241007a1499c68e14 to your computer and use it in GitHub Desktop.
Save danielgarbien/849467304a114fb241007a1499c68e14 to your computer and use it in GitHub Desktop.
extension KeyedDecodingContainer {
func decode<T: NSCoding>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T? {
let data = try decode(Data.self, forKey: key)
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? T ?? nil
}
}
extension KeyedEncodingContainer {
mutating func encode(_ value: NSCoding, forKey key: KeyedEncodingContainer<K>.Key) throws {
let data = NSKeyedArchiver.archivedData(withRootObject: value)
try encode(data, forKey: key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment