-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStorage.swift
37 lines (32 loc) · 984 Bytes
/
Storage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// Storage.swift
// SharedDefaults
//
// Created by arshak on 16.04.23.
//
import Foundation
public struct Storage {
public var local: UserDefaults
public var cloud: NSUbiquitousKeyValueStore?
public var shouldUseCloud: Bool
public init(local: UserDefaults, cloud: NSUbiquitousKeyValueStore?, shouldUseCloud: Bool) {
self.local = local
self.cloud = cloud
self.shouldUseCloud = shouldUseCloud
}
public func get<T>(_ key: String, defaultValue: T) -> T {
if shouldUseCloud, let cloudValue = cloud?.object(forKey: key) as? T {
return cloudValue
} else if let localValue = local.object(forKey: key) as? T {
return localValue
} else {
return defaultValue
}
}
public func set<T>(_ value: T, forKey key: String) {
local.set(value, forKey: key)
if shouldUseCloud {
cloud?.set(value, forKey: key)
}
}
}