This commit is contained in:
Ivan Smolin 2018-09-14 19:45:01 +03:00
parent 2e70e8248d
commit b796702b4e
1 changed files with 3 additions and 9 deletions

View File

@ -11,14 +11,6 @@ private typealias JSONObject = [String: Any]
public extension UserDefaults {
private func storedValue<ST>(forKey key: String) throws -> ST {
guard let objectForKey = object(forKey: key) else {
throw UserDefaultsError.noSuchValue(key: key)
}
return try cast(objectForKey) as ST
}
/// Returns the object with specified type associated with the first occurrence of the specified default.
///
/// - Parameters:
@ -28,7 +20,9 @@ public extension UserDefaults {
/// or throw exception if the key was not found.
/// - Throws: One of cases in UserDefaultsError
func object<T: Decodable>(forKey key: String, decoder: JSONDecoder = JSONDecoder()) throws -> T {
let storedData = try storedValue(forKey: key) as Data
guard let storedData = data(forKey: key) else {
throw UserDefaultsError.noSuchValue(key: key)
}
do {
return try decoder.decode(T.self, from: storedData)