Created
October 29, 2019 06:55
-
-
Save danielgarbien/849467304a114fb241007a1499c68e14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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