young
(rtSwift)
1
SwiftUI.AppStorage comes with these two:
init(wrappedValue: Value, _ key: String, store: UserDefaults? = nil) where Value : RawRepresentable, Value.RawValue == Int
init(wrappedValue: Value, _ key: String, store: UserDefaults? = nil) where Value : RawRepresentable, Value.RawValue == String
I want to add:
init(wrappedValue: Value, _ key: String, store: UserDefaults? = nil) where Value : RawRepresentable, Value.RawValue == Double
But I can't figure out how to do this, appears not possible?
I want to store Date in AppStorage by extending Date to conform to RawRepresentable. I want this RawRepresentable.RawValue to be type Double, not String to avoid format/parsing String in and out.
instead of this:
extension Date: RawRepresentable {
public var rawValue: String {
timeIntervalSinceReferenceDate.description
}
public init?(rawValue: String) {
guard let d = Double(rawValue) else {
print("⚠️ Date failed to parse rawValue \"\(rawValue)\" as a TimeInterval(aka Double)")
return nil
}
self.init(timeIntervalSinceReferenceDate: d)
}
}
do this:
extension Date: RawRepresentable {
public var rawValue: Double {
timeIntervalSinceReferenceDate
}
public init?(rawValue: Double) {
self.init(timeIntervalSinceReferenceDate: rawValue)
}
}