diff --git a/Sources/Extensions/Foundation/UserDefaults/UserDefaults+Codable.swift b/Sources/Extensions/Foundation/UserDefaults/UserDefaults+Codable.swift index 210e0c89..c1036e0b 100644 --- a/Sources/Extensions/Foundation/UserDefaults/UserDefaults+Codable.swift +++ b/Sources/Extensions/Foundation/UserDefaults/UserDefaults+Codable.swift @@ -11,14 +11,6 @@ private typealias JSONObject = [String: Any] public extension UserDefaults { - private func storedValue(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(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)