Created
August 18, 2017 08:29
-
-
Save Dimillian/f1a68c968836702c49439ba9013f44c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
private func decodeObject<T: Codable>(key: String) -> T? { | |
guard objectExist(key) else { return nil } | |
let path = URL(fileURLWithPath: fullPath(key)) | |
do { | |
let data = try Data(contentsOf: path) | |
let decoder = PropertyListDecoder() | |
let object = try decoder.decode(T.self, from: data) | |
return object | |
} catch { | |
print("Decoding error: \(error)") | |
} | |
return nil | |
} | |
private func makeEncoder() -> PropertyListEncoder { | |
let encoder = PropertyListEncoder() | |
encoder.outputFormat = .binary | |
return encoder | |
} | |
private func encodeObject<T: Codable>(object: T, key: String) { | |
do { | |
let encoder = self.makeEncoder() | |
let data = try encoder.encode(object) | |
let path = URL(fileURLWithPath: self.fullPath(key)) | |
try data.write(to: path, options: .atomic) | |
} catch { | |
print("Encoding error: \(error)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment