-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDManager.swift
executable file
·101 lines (87 loc) · 2.63 KB
/
UDManager.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// UDManager.swift
// Extensions
//
// Created by 강동영 on 2023/11/13.
//
import Foundation
enum TypeCodeSampleEnum: String {
case wait = "W0001"
case success = "S0000"
case failed = "F001"
}
@propertyWrapper
struct UDOptionalWrapper<T> {
private let ud = UserDefaults.standard
var key: String
var wrappedValue: T? {
get {
return ud.value(forKey: key) as? T
}
set {
ud.setValue(newValue, forKey: key)
ud.synchronize()
}
}
}
@propertyWrapper
struct UDWrapper<T> {
private let ud = UserDefaults.standard
var key: String
var defaultValue: T
var wrappedValue: T {
get {
return ud.value(forKey: key) as? T ?? defaultValue
}
set {
ud.setValue(newValue, forKey: key)
ud.synchronize()
}
}
}
struct UDManager {
@UDWrapper(key: "uuid", defaultValue: "")
static var uuid: String
@UDOptionalWrapper(key: UDStringKey.name.rawValue)
static var name: String?
@UDWrapper(key: UDStringKey.isFirst.rawValue, defaultValue: false)
static var isFirst: Bool
@UDWrapper(key: UDStringKey.tellNumber.rawValue, defaultValue: 0)
static var tellNumber: Int
// Custom Type
static var cateExpCode: TypeCodeSampleEnum {
get {
guard
let strValue = UserDefaults.standard.value(forKey: UDStringKey.typeCodeEnum.rawValue) as? String,
let enumValue = TypeCodeSampleEnum(rawValue: strValue)
else { return .wait }
return enumValue
}
set {
let ud = UserDefaults.standard
ud.setValue(newValue.rawValue, forKey: UDStringKey.typeCodeEnum.rawValue)
}
}
// tuple for userDefaults
static var lastFilterCateInfo: (name: String, tellNum: Int)? {
get {
let ud = UserDefaults.standard
let baseKey = UDStringKey.userInfo.rawValue
let nameKey = baseKey + "_Key"
let numKey = baseKey + "_Num"
guard
let nm = ud.value(forKey: nameKey) as? String,
let num = ud.value(forKey: numKey) as? Int
else { return nil }
return (nm,num)
}
set {
let ud = UserDefaults.standard
let baseKey = UDStringKey.userInfo.rawValue
let nameKey = baseKey + "_Key"
let numKey = baseKey + "_Num"
ud.setValue(newValue?.name, forKey: nameKey)
ud.setValue(newValue?.tellNum, forKey: numKey)
}
}
}