KeyValueStorage
public protocol KeyValueStorage
This protocol describes the interface of a key value storage, where the keys
are Strings and the values are Data objects.
-
A key is the identifier for a specific value stored in the key-value storage. The key is unique.
Declaration
Swift
typealias Key = String -
A value can contain arbitrary data and is identified by a key.
Declaration
Swift
typealias Value = Bytes -
Store a value in the key-value storage for a given key.
Example
storage.store(key: "name", value: Data("John Doe".utf8)) { optionalError in if let error = optionalError { // TODO Handle error } }Declaration
Parameters
valueThe value that should be stored.
keyThe key that identifies the value.
finishedA closure that is called asynchronuously once the operation is finished.
errorAn optional error that might have occurred during storing.
-
Retrieve a value from the key-value storage for a given key.
Example
storage.retrieve(for: "name") { optionalValue, optionalError in precondition((optionalValue != nil) == (optionalError != nil)) guard let value = optionalValue else { let error = optionalError! // TODO Handle error return } // Success, do something with `value` }Postcondition
(
value=nil) ⊻ (error=nil)Declaration
Parameters
keyThe key that identifies the value.
finishedA closure that is called asynchronuously once the operation is finished.
valueThe value if no error occurred,
nilelse.errorAn optional error that might have occurred during storing.
-
Remove the value from the key-value storage for a given key.
Example
storage.remove(key: "name") { optionalError in if let error = optionalError { // TODO Handle error } }Declaration
Swift
func remove(for key: Key, finished: @escaping (_ error: Error?) -> Void)Parameters
keyThe key that identifies the value.
finishedA closure that is called asynchronuously once the operation is finished.
errorAn optional error that might have occurred during storing.
View on GitHub
KeyValueStorage Protocol Reference