-
Notifications
You must be signed in to change notification settings - Fork 2
/
SystemDefaults.swift
37 lines (31 loc) · 1.09 KB
/
SystemDefaults.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
import Foundation
import PlistLib
public final class SystemDefaults: Defaults {
private let userDefaults: UserDefaults
public struct CannotCreateUserDefaultsError: CustomStringConvertible, Error {
let suiteName: String
public var description: String {
"Не могу создать user defaults для набора \(suiteName)"
}
}
public init(
suiteName: String
) throws {
guard let userDefaults = UserDefaults(suiteName: suiteName) else {
throw CannotCreateUserDefaultsError(suiteName: suiteName)
}
self.userDefaults = userDefaults
}
public func entryForKey(_ key: String) throws -> PlistEntry? {
if let object = userDefaults.object(forKey: key) {
return try PlistEntry.create(fromAny: object)
}
return nil
}
public func set(entry: PlistEntry?, key: String) {
userDefaults.set(entry?.toPlistObject(), forKey: key)
}
public func removeEntryForKey(_ key: String) {
userDefaults.removeObject(forKey: key)
}
}