Skip to content

Instantly share code, notes, and snippets.

@ihamadfuad
Created February 21, 2022 07:19
Show Gist options
  • Save ihamadfuad/8814d0fe91f492f59be83b5147b1ed63 to your computer and use it in GitHub Desktop.
Save ihamadfuad/8814d0fe91f492f59be83b5147b1ed63 to your computer and use it in GitHub Desktop.
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