Add integer methods

This commit is contained in:
ismailgulek
2020-09-28 16:17:13 +03:00
parent d6c3b6f95d
commit 442868f46a
3 changed files with 23 additions and 0 deletions

View File

@@ -23,11 +23,13 @@ protocol KeyValueStore {
func set(_ value: Data?, forKey key: KeyValueStoreKey) throws
func set(_ value: String?, forKey key: KeyValueStoreKey) throws
func set(_ value: Bool?, forKey key: KeyValueStoreKey) throws
func set(_ value: Int?, forKey key: KeyValueStoreKey) throws
// getters
func data(forKey key: KeyValueStoreKey) throws -> Data?
func string(forKey key: KeyValueStoreKey) throws -> String?
func bool(forKey key: KeyValueStoreKey) throws -> Bool?
func integer(forKey key: KeyValueStoreKey) throws -> Int?
// remove
func removeObject(forKey key: KeyValueStoreKey) throws

View File

@@ -76,6 +76,15 @@ extension KeychainStore: KeyValueStore {
try keychain.set(value, key: key)
}
func set(_ value: Int?, forKey key: KeyValueStoreKey) throws {
guard let value = value else {
try removeObject(forKey: key)
return
}
try keychain.set(String(value), key: key)
}
// getters
func data(forKey key: KeyValueStoreKey) throws -> Data? {
return try keychain.getData(key)
@@ -89,6 +98,10 @@ extension KeychainStore: KeyValueStore {
return try keychain.getBool(key)
}
func integer(forKey key: KeyValueStoreKey) throws -> Int? {
return try Int(keychain.getString(key) ?? "")
}
// remove
func removeObject(forKey key: KeyValueStoreKey) throws {
try keychain.remove(key)

View File

@@ -49,6 +49,10 @@ extension MemoryStore: KeyValueStore {
setObject(value, forKey: key)
}
func set(_ value: Int?, forKey key: KeyValueStoreKey) throws {
setObject(value, forKey: key)
}
// getters
func data(forKey key: KeyValueStoreKey) throws -> Data? {
return object(forKey: key) as? Data
@@ -62,6 +66,10 @@ extension MemoryStore: KeyValueStore {
return object(forKey: key) as? Bool
}
func integer(forKey key: KeyValueStoreKey) throws -> Int? {
return object(forKey: key) as? Int
}
// remove
func removeObject(forKey key: KeyValueStoreKey) {
map.removeValue(forKey: key)