Skip to content

Commit

Permalink
Fix default value not working for primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
ktiays committed Mar 31, 2024
1 parent 2563a68 commit 6ffabf9
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Sources/CyanUtils/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ public protocol ConstructibleFromDefaults {
static func from(_ defaults: UserDefaults, with key: String) -> Self?
}

private func getPrimitiveDefaultsValue<T>(of type: T.Type,
from defaults: UserDefaults,
with key: String,
objCType: String) -> T? {
guard let number = defaults.object(forKey: key) as? NSNumber else {
return nil
}
let actualObjCType = String(cString: number.objCType)
guard objCType == actualObjCType else {
return nil
}
return number as? T
}

extension Int: ConstructibleFromDefaults {
public static func from(_ defaults: UserDefaults, with key: String) -> Self? {
defaults.integer(forKey: key)
return getPrimitiveDefaultsValue(of: Int.self, from: defaults, with: key, objCType: "q")
}
}

extension Float: ConstructibleFromDefaults {
public static func from(_ defaults: UserDefaults, with key: String) -> Float? {
defaults.float(forKey: key)
return getPrimitiveDefaultsValue(of: Float.self, from: defaults, with: key, objCType: "f")
}
}

extension Double: ConstructibleFromDefaults {
public static func from(_ defaults: UserDefaults, with key: String) -> Double? {
defaults.double(forKey: key)
return getPrimitiveDefaultsValue(of: Double.self, from: defaults, with: key, objCType: "d")
}
}

Expand All @@ -37,7 +51,7 @@ extension String: ConstructibleFromDefaults {

extension Bool: ConstructibleFromDefaults {
public static func from(_ defaults: UserDefaults, with key: String) -> Self? {
defaults.bool(forKey: key)
return getPrimitiveDefaultsValue(of: Bool.self, from: defaults, with: key, objCType: "c")
}
}

Expand Down

0 comments on commit 6ffabf9

Please sign in to comment.