diff --git a/Sources/EFStorageKeychainAccess/EFStorage+KeychainAccess.swift b/Sources/EFStorageKeychainAccess/EFStorage+KeychainAccess.swift index d02a675..9ea6396 100644 --- a/Sources/EFStorageKeychainAccess/EFStorage+KeychainAccess.swift +++ b/Sources/EFStorageKeychainAccess/EFStorage+KeychainAccess.swift @@ -34,7 +34,7 @@ public enum AsIsKeychainAccessStorable { } public protocol KeychainAccessStorable { - func asKeychainStorable() -> Result + func asKeychainAccessStorable() -> Result static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? } @@ -60,7 +60,7 @@ public class EFStorageKeychainAccessRef: EFSing try? storage.remove(key) return } - switch newValue.asKeychainStorable() { + switch newValue.asKeychainAccessStorable() { case .success(.string(let string)): do { try storage.set(string, key: key) diff --git a/Sources/EFStorageKeychainAccess/KeychainAccess+NSCoding.swift b/Sources/EFStorageKeychainAccess/KeychainAccess+NSCoding.swift index f7d74ce..ba2318d 100644 --- a/Sources/EFStorageKeychainAccess/KeychainAccess+NSCoding.swift +++ b/Sources/EFStorageKeychainAccess/KeychainAccess+NSCoding.swift @@ -9,7 +9,7 @@ import KeychainAccess import Foundation public extension KeychainAccessStorable where Self: NSCoding { - func asKeychainStorable() -> Result { + func asKeychainAccessStorable() -> Result { return .success(.data(NSKeyedArchiver.archivedData(withRootObject: self))) } static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? { diff --git a/Sources/EFStorageKeychainAccess/KeychainAccess+Primitives.swift b/Sources/EFStorageKeychainAccess/KeychainAccess+Primitives.swift index c71bfea..c20f8e2 100644 --- a/Sources/EFStorageKeychainAccess/KeychainAccess+Primitives.swift +++ b/Sources/EFStorageKeychainAccess/KeychainAccess+Primitives.swift @@ -9,7 +9,7 @@ import KeychainAccess import Foundation extension String: KeychainAccessStorable { - public func asKeychainStorable() -> Result { + public func asKeychainAccessStorable() -> Result { return .success(.string(self)) } public static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? { @@ -18,7 +18,7 @@ extension String: KeychainAccessStorable { } extension Data: KeychainAccessStorable { - public func asKeychainStorable() -> Result { + public func asKeychainAccessStorable() -> Result { return .success(.data(self)) } public static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? { diff --git a/Sources/EFStorageKeychainAccess/KeychainAccess+RawRepresentable.swift b/Sources/EFStorageKeychainAccess/KeychainAccess+RawRepresentable.swift index 3d0c490..eebb676 100644 --- a/Sources/EFStorageKeychainAccess/KeychainAccess+RawRepresentable.swift +++ b/Sources/EFStorageKeychainAccess/KeychainAccess+RawRepresentable.swift @@ -8,8 +8,8 @@ import KeychainAccess public extension KeychainAccessStorable where Self: RawRepresentable, Self.RawValue: KeychainAccessStorable { - func asKeychainStorable() -> Result { - return rawValue.asKeychainStorable() + func asKeychainAccessStorable() -> Result { + return rawValue.asKeychainAccessStorable() } static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? { return RawValue.fromKeychain(keychain, forKey: key) diff --git a/Sources/EFStorageUserDefaults/EFStorage+UserDefaults.swift b/Sources/EFStorageUserDefaults/EFStorage+UserDefaults.swift index eeb5d5f..bb4e7ae 100644 --- a/Sources/EFStorageUserDefaults/EFStorage+UserDefaults.swift +++ b/Sources/EFStorageUserDefaults/EFStorage+UserDefaults.swift @@ -18,7 +18,7 @@ extension UserDefaults: EFUnderlyingStorage { /// NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary public protocol UserDefaultsStorable { - func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! + func asUserDefaultsStorable() -> Result static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? } @@ -43,18 +43,19 @@ public class EFStorageUserDefaultsRef: EFSingleIn guard let newValue = content else { return storage.removeObject(forKey: key) } - if let storable = newValue.asUserDefaultsStorable() { + switch newValue.asUserDefaultsStorable() { + case .success(let storable): storage.set(storable, forKey: key) - } else { - onConversionFailure(for: newValue) + case .failure(let error): + onConversionFailure(for: newValue, dueTo: error) } } } - public dynamic func onConversionFailure(for content: Content) { + public dynamic func onConversionFailure(for content: Content, dueTo error: Error) { assertionFailure(""" \(content) of type \(type(of: content)) \ - is not storable in user defaults. + is not storable in user defaults because \(error). """) } diff --git a/Sources/EFStorageUserDefaults/UserDefaults+Codable.swift b/Sources/EFStorageUserDefaults/UserDefaults+Codable.swift index 071a101..060b485 100644 --- a/Sources/EFStorageUserDefaults/UserDefaults+Codable.swift +++ b/Sources/EFStorageUserDefaults/UserDefaults+Codable.swift @@ -8,17 +8,8 @@ import Foundation public extension UserDefaultsStorable where Self: Codable { - func asUserDefaultsStorable() -> UserDefaultsStorable! { - do { - return try JSONEncoder().encode(self) - } catch { - return onConversionFailure(dueTo: error) - } - } - - dynamic func onConversionFailure(dueTo error: Error) -> UserDefaultsStorable! { - assertionFailure(error.localizedDescription) - return nil + func asUserDefaultsStorable() -> Result { + return Result { try JSONEncoder().encode(self) } } static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? { diff --git a/Sources/EFStorageUserDefaults/UserDefaults+NSCoding.swift b/Sources/EFStorageUserDefaults/UserDefaults+NSCoding.swift index 06f671e..5e660f8 100644 --- a/Sources/EFStorageUserDefaults/UserDefaults+NSCoding.swift +++ b/Sources/EFStorageUserDefaults/UserDefaults+NSCoding.swift @@ -8,8 +8,8 @@ import Foundation public extension UserDefaultsStorable where Self: NSCoding { - func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! { - return NSKeyedArchiver.archivedData(withRootObject: self) + func asUserDefaultsStorable() -> Result { + return NSKeyedArchiver.archivedData(withRootObject: self).asUserDefaultsStorable() } static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? { guard let data = userDefaults.data(forKey: key) else { return nil } diff --git a/Sources/EFStorageUserDefaults/UserDefaults+Primitives.swift b/Sources/EFStorageUserDefaults/UserDefaults+Primitives.swift index d573979..ee6dd63 100644 --- a/Sources/EFStorageUserDefaults/UserDefaults+Primitives.swift +++ b/Sources/EFStorageUserDefaults/UserDefaults+Primitives.swift @@ -8,8 +8,8 @@ import Foundation extension URL: UserDefaultsStorable { - public func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! { - return absoluteString + public func asUserDefaultsStorable() -> Result { + return absoluteString.asUserDefaultsStorable() } public static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> URL? { return userDefaults.url(forKey: key) @@ -20,8 +20,8 @@ extension URL: UserDefaultsStorable { public protocol AsIsUserDefaultsStorable: UserDefaultsStorable { } extension AsIsUserDefaultsStorable { - public func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! { - return self + public func asUserDefaultsStorable() -> Result { + return .success(self) } public static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? { guard let object = userDefaults.object(forKey: key) else { return nil } diff --git a/Sources/EFStorageUserDefaults/UserDefaults+RawRepresentable.swift b/Sources/EFStorageUserDefaults/UserDefaults+RawRepresentable.swift index 866590d..d7998bb 100644 --- a/Sources/EFStorageUserDefaults/UserDefaults+RawRepresentable.swift +++ b/Sources/EFStorageUserDefaults/UserDefaults+RawRepresentable.swift @@ -8,7 +8,7 @@ import Foundation public extension UserDefaultsStorable where Self: RawRepresentable, Self.RawValue: UserDefaultsStorable { - func asUserDefaultsStorable() -> UserDefaultsStorable! { + func asUserDefaultsStorable() -> Result { return rawValue.asUserDefaultsStorable() } static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? {