SecureKeyValueStore
open class SecureKeyValueStore : KeyValueStoreProtocol
extension SecureKeyValueStore: DataStoring
Secure Key-Value Store
Similar to the SecureDatabaseStore
, you can create instances of the SecureKeyValueStore
as follows.
let secureKeyValueStore = SecureKeyValueStore()
try secureKeyValueStore.open(with: "your_encryption_key")
It is also possible to create more SecureKeyValueStore
s sharing the same database file. In this can specify a separate name for the store.
Shared the same database is not thread safe. It is the task of the caller to make it thread safe.
// it is advisable to name all the stores if multiple store used in one file
let firstStore = SecureKeyValueStore(name:"FirstStore")
var secondStore = SecureKeyValueStore()
do {
// initialization throws only when the firstStore is open already
try SecureKeyValueStore(databaseStore: firstStore.secureDatabaseStore, name:"secondKeyValueStore")
} catch {
// handle error
return
}
// open one of the stores
do {
try firstStore.open(with: "any passcode")
} catch {
// handle error
return
}
Since the init of the store will throw only when the first database is already open and there isn’t schema created already and the schema creation fails it is possible to create the store with try! Simplified form:
let firstStore = SecureKeyValueStore(name:"FirstStore")
let secondStore = try! SecureKeyValueStore(databaseStore: firstStore.secureDatabaseStore, name:"secondKeyValueStore")
// open one of the stores
do {
try firstStore.open(with: "any passcode")
} catch {
// handle error
return
}
-
secureDatabaseStore instance
Declaration
Swift
public let secureDatabaseStore: SecureDatabaseStore
-
default filename for database
Declaration
Swift
public static let defaultDatabaseFileName: String
-
the name of the KeyValueStore
Declaration
Swift
open var storeName: String
-
Initialize a new
SecureKeyValueStore
in the default location with the specified or default database file name.Declaration
Swift
public convenience init(databaseFileName: String = defaultDatabaseFileName, name: String = SecureStoreDefaultStoreName)
Parameters
databaseFileName
a database file name with a default value
name
the name of the KeyValueStore - used to create a separate Database table in the
SecureDatabaseStore
-
Initialize a new
SecureDatabaseStore
in the specified path.Declaration
Swift
public convenience init(fullDatabasePath: String, name: String = SecureStoreDefaultStoreName)
Parameters
fullDatabasePath
a full path of the database file name
storeName
the name of the KeyValueStore - used to create a separate Database table in the
SecureDatabaseStore
-
Initializes a store using an other existing store. This way the same database file will be used. Make sure to use unique names for the stores. The name given will be used to create a separate table so use only characters allowed to create a table name. If the
databaseStore
already exists the init tries to create the table for this store if it doesn’t exist already. If this creation fails the init fails. If thedatabaseStore
is not open the init will never fail. The table for the store will be created during theopen
of the hostdatabaseStore
. If the table creation fails during the open, the open method will fail and the database remains closed.Declaration
Swift
public convenience init(databaseStore: SecureDatabaseStore, name: String = SecureStoreDefaultStoreName) throws
Parameters
databaseStore
an existing store
name
unique name of the store in this database
-
Opens the
SecureStore
with the respective encryption key. This call sets the encryption key of a store if the store is opened for the first time and if data is written afterwards. The underlying store is opened for read and write access and it is created at the specified path if it does not already exist.Throws
SecureStorageError.AuthenticationFailed
if an error occured while setting the encryption key and if the encyption key is wrong or an empty string.Throws
SecureStorageError.OpenFailed
if something went wrong while opening and/or creating the store. Error codes correspond to sqlite3 error codes if the underlying store is a sqlite database (see https://www.sqlite.org/c3ref/c_abort.html).Note
Please make sure to set thedelegate
before you callopen(with encryptionKey: String)
if you want to receive thedidCreateStore
callback.Note
The actual database encryption key is drived from the StringencryptionKey
with PBKDF2.Note
In case the store is already opened the method doesn’t have any effect and it won’t validate the passcode.Declaration
Swift
open func open(with encryptionKey: String) throws
Parameters
encryptionKey
the secure store’s encryption key as String.
-
Opens the
SecureStore
with the respective encryption key. The underlying store is opened for read and write access. The store at the specified path is created if it does not exist.Throws
SecureStorageError.AuthenticationFailed
if an error occured while setting the encryption key and if the encyption key is wrong or an empty string.Throws
SecureStorageError.OpenFailed
if something went wrong while opening and/or creating the store. Error codes correspond to sqlite3 error codes if the underlying store is a sqlite database (see https://www.sqlite.org/c3ref/c_abort.html).Warning
The binaryencryptionKeyData
data is used directly as the database encryption key. The must be exactly 256 bit in length.Note
Please make sure to set thedelegate
before you callopen(with encryptionKey: Data)
if you want to receive thedidCreateStore
callback.Declaration
Swift
open func open(with encryptionKeyData: Data) throws
Parameters
ecryptionKeyData
the secure store’s binary encryption key as Data. It must be exactly 256 bit in length.
-
Checks whether the store is open.
Declaration
Swift
open func isOpen() -> Bool
Return Value
true
if the store is open,false
otherwise. -
Closes the store and therefore the
SecureStore
is no longer accessible without opening it again with its encryption key.Declaration
Swift
open func close()
-
Changes the encryption key of the
SecureStore
if it has been opened. OtherwiseSecureStorageError.Closed
is thrown and the encryption key remains unchanged.Changing the key means re-encryption of the whole database which can take a long time on larger databases.
Throws
SecureStorageError.Closed
if the store is locked with its encryption key.Throws
SecureStorageError.EncryptionKeyChangeFailed
if an error occured while changing the encryption key.Declaration
Swift
open func changeEncryptionKey(with newEncryptionKey: String) throws
Parameters
newEncryptionKey
the
SecureStore
’s new encryption key as String. -
Changes the encryption key of the
SecureStore
if it has been opened. OtherwiseSecureStorageError.Closed
is thrown and the encryption key remains unchanged.Changing the key means re-encryption of the whole database which can take a long time on larger databases.
Throws
SecureStorageError.Closed
if the store is locked with its encryption key.Throws
SecureStorageError.EncryptionKeyChangeFailed
if an error occured while changing the encryption key.Declaration
Swift
open func changeEncryptionKey(with newEncryptionKeyData: Data) throws
Parameters
newEncryptionKeyData
the
SecureStore
’s new encryption key.
-
Inserts a
SQLiteDatatypeBridgeable
compliant value into the mapping, replacing any existing value for the given key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func put<T>(_ value: T?, forKey key: String) throws where T : SQLiteDatatypeBridgeable
Parameters
key
a String
value
has to be a
SQLiteDatatypeBridgeable
compliant object -
Inserts a
NSCoding
compliant value into the mapping, replacing any existing value for the given key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
This method serializes the value before it is inserted into the underlying store. Therefore, the values must be read with theget<T: NSCoding>(_ key: String) throws -> T?
in order to deserialize them properly. It is not possible to read values with specific get methods likegetString(key: String) throws -> String?
.Declaration
Swift
open func put<T>(_ value: T?, forKey key: String) throws where T : NSCoding
Parameters
key
a String
value
has to be a
NSCoding
compliant object -
Associates all specified values with their respective key. The effect of this call is equivalent to that of calling put(k, v) on this SecureKeyValueStore once for each mapping from key k to value v in the specified map.
Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func putAll(_ keyValueDict: [String : NSCoding?]) throws
Parameters
keyValueDict
mappings to be stored
-
Returns the
SQLiteDatatypeBridgeable
compliant value to which the specified key is mapped, ornil
if no mapping for this key exists.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func get<T>(_ key: String) throws -> T? where T : SQLiteDatatypeBridgeable
Parameters
key
a String.
Return Value
the value as decoded value or
nil
. -
Returns the
NSCoding
compliant value to which the specified key is mapped, ornil
if no mapping for this key exists.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Note
This method deserializes the value when it is read from the underlying store. Therefore, the values must have been set with theput(value: NSCoding?, forKey key: String) throws
method in order to be serialized properly. It is not possible to read values with this method that have been set with the specific put methods likeputString(value: String?, forKey key: String) throws
.Declaration
Swift
open func get<T>(_ key: String) throws -> T? where T : NSCoding
Parameters
key
a String.
Return Value
the value as decoded value or
nil
. -
Returns the
String
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func getString(_ key: String) throws -> String?
Parameters
key
a String
Return Value
a String value or
nil
-
Returns the
Int8
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getInt8(_ key: String) throws -> Int8?
Parameters
key
a String
Return Value
an
Int8
value ornil
-
Returns the
Int16
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getInt16(_ key: String) throws -> Int16?
Parameters
key
a String
Return Value
an
Int16
value ornil
-
Returns the
Int32
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getInt32(_ key: String) throws -> Int32?
Parameters
key
a String
Return Value
an
Int32
value ornil
-
Returns the
Int64
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getInt64(_ key: String) throws -> Int64?
Parameters
key
a String
Return Value
an
Int64
value ornil
-
Returns the
Int
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getInt(_ key: String) throws -> Int?
Parameters
key
a String
Return Value
an
Int
value ornil
-
Returns the
UInt8
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getUInt8(_ key: String) throws -> UInt8?
Parameters
key
a String
Return Value
an
UInt8
value ornil
-
Returns the UInt16 value associated with the given key, or
nil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getUInt16(_ key: String) throws -> UInt16?
Parameters
key
a String
Return Value
an
UInt16
value ornil
-
Returns the
UInt32
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getUInt32(_ key: String) throws -> UInt32?
Parameters
key
a String
Return Value
an
UInt32
value ornil
-
Returns the
UInt64
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getUInt64(_ key: String) throws -> UInt64?
Parameters
key
a String
Return Value
an
UInt64
value ornil
-
Returns the
UInt
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getUInt(_ key: String) throws -> UInt?
Parameters
key
a String
Return Value
an
UInt
value ornil
-
Returns the
Float
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getFloat(_ key: String) throws -> Float?
Parameters
key
a String
Return Value
a
Float
value ornil
-
Returns the
Double
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getDouble(_ key: String) throws -> Double?
Parameters
key
a String
Return Value
a
Double
value ornil
-
Returns the
Bool
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Note
Conversions between different types of numbers may produce erroneous results. Such as putting anInt64.max
for a specific key and getting this value withtype: Int8
. Conversions are handled in accordance to the behaviour ofNSNumber
. Further information can be found at https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html.Declaration
Swift
open func getBool(_ key: String) throws -> Bool?
Parameters
key
a String
Return Value
a
Bool
value ornil
-
Returns the
NSData
value associated with the given key, ornil
if no mapping of the desired type exists for the given key or anil
value is explicitly associated with the key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func getData(_ key: String) throws -> Data?
Parameters
key
a String
Return Value
a
NSData
value ornil
-
MARK: - Getting Values with Default Value Returns the
NSCoding
compliant value associated with the given key, ordefaultValue
if no mapping of the desired type exists for the given key or if anil
value is explicitly associated with the given key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func get<T>(_ key: String, defaultValue: T?) throws -> T? where T : NSCoding
Parameters
key
a String.
defaultValue
returned if key does not exist or if a
nil
value is associated with the given key.Return Value
the decoded value associated with the given key, or
defaultValue
if no valid object is currently mapped to that key.
-
Returns the
SQLiteDatatypeBridgeable
compliant value associated with the given key, ordefaultValue
if no mapping of the desired type exists for the given key or if anil
value is explicitly associated with the given key.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func get<T>(_ key: String, defaultValue: T?) throws -> T? where T : SQLiteDatatypeBridgeable
Parameters
key
a String.
defaultValue
returned if key does not exist or if a
nil
value is associated with the given key.Return Value
the decoded value associated with the given key, or
defaultValue
if no valid object is currently mapped to that key.
-
Returns a Set containing the Strings used as keys in this
SecureKeyValueStore
.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func keys() throws -> Set<String>
Return Value
a
Set
of keys. -
Returns whether the given key is contained in the mapping of this
SecureKeyValueStore
.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func hasKey(_ key: String) throws -> Bool
Parameters
key
a String.
Return Value
true
if the key is part of the mapping,false
otherwise
-
Returns the number of mappings contained in this
SecureKeyValueStore
.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func count() throws -> Int
Return Value
the number of mappings as an Int.
-
Returns whether the mapping of this
SecureKeyValueStore
is empty.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func isEmpty() throws -> Bool
Return Value
true
if the mapping of this SecureKeyValueStore is empty,false
otherwise.
-
Removes the entry with the given key from the mapping of this
SecureKeyValueStore
.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
@discardableResult open func remove(_ key: String) throws -> Bool
Parameters
key
a String.
Return Value
true
if the key entry was successfully removed,false
if no mapping for the key exists. -
Removes all elements from the mapping of this
SecureKeyValueStore
.Throws
SecureStorageError.Closed
if the SecureKeyValueStore is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func removeAll() throws
-
Implementation for DataStoring protocol’s put function
Throws
SecureStorageError.Closed
if theDataStoring
compliant object is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
public func put(data: Data, for key: String) throws
Parameters
data
data to put in the storage
key
a String which will be the key for the data in the storage
-
Implementation for DataStoring protocol’s data(for:) function
Throws
SecureStorageError.Closed
if theDataStoring
compliant object is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
public func data(for key: String) throws -> Data?
Parameters
key
a String
Return Value
Data if there is data for the key, or nil if not found
-
Implementation for DataStoring protocol’s removeData(for:) function
Throws
SecureStorageError.Closed
if theDataStoring
compliant object is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
public func removeData(for key: String) throws
Parameters
key
a String to find data in the storage