Created
February 21, 2022 07:19
-
-
Save ihamadfuad/8814d0fe91f492f59be83b5147b1ed63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias Value = T | |
let key: String | |
@State private var value: Value? | |
init(wrappedValue: Value? = nil, _ key: String) { | |
self.key = key | |
var initialValue = wrappedValue | |
do { | |
try Keychain().get(key) { attributes in | |
if let attributes = attributes, | |
let data = attributes.data { | |
do { | |
let decoded = try JSONDecoder().decode(Value.self, from: data) | |
initialValue = decoded | |
} catch let error { | |
print("[KeychainStorage] Keychain().get(\(key)) - \(error.localizedDescription)") | |
} | |
} | |
} | |
} catch let error { | |
fatalError("\(error)") | |
} | |
self._value = State<Value?>(initialValue: initialValue) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment