Change KeyValueStore api

This commit is contained in:
ismailgulek
2020-07-28 18:21:39 +03:00
parent 3937f706aa
commit 8bd8e99f72
4 changed files with 90 additions and 31 deletions
@@ -17,11 +17,11 @@
import Foundation
@objcMembers
class MemoryStore: KeyValueStore {
class MemoryStore {
private var map: Dictionary<KeyValueStoreKey, Any> = [:]
func set(_ value: Any?, forKey key: KeyValueStoreKey) {
private func set(_ value: Any?, forKey key: KeyValueStoreKey) {
if let value = value {
map[key] = value
} else {
@@ -29,10 +29,41 @@ class MemoryStore: KeyValueStore {
}
}
func object(forKey key: KeyValueStoreKey) -> Any? {
private func object(forKey key: KeyValueStoreKey) -> Any? {
return map[key]
}
}
extension MemoryStore: KeyValueStore {
// setters
func setData(_ value: Data?, forKey key: KeyValueStoreKey) throws {
set(value, forKey: key)
}
func setString(_ value: String?, forKey key: KeyValueStoreKey) throws {
set(value, forKey: key)
}
func setBool(_ value: Bool?, forKey key: KeyValueStoreKey) throws {
set(value, forKey: key)
}
// getters
func data(forKey key: KeyValueStoreKey) throws -> Data? {
return object(forKey: key) as? Data
}
func string(forKey key: KeyValueStoreKey) throws -> String? {
return object(forKey: key) as? String
}
func bool(forKey key: KeyValueStoreKey) throws -> Bool? {
return object(forKey: key) as? Bool
}
// remove
func removeObject(forKey key: KeyValueStoreKey) {
map.removeValue(forKey: key)
}