9
|
1 //
|
19
|
2 // UserController.swift
|
9
|
3 // GeoQuiz
|
|
4 //
|
|
5 // Created by Dennis Concepción Martín on 7/10/22.
|
|
6 //
|
|
7
|
|
8 import Foundation
|
|
9
|
19
|
10 class UserController: ObservableObject {
|
|
11 @Published var data = UserDataModel() {
|
9
|
12 didSet {
|
14
|
13 if let userDataEncoded = try? JSONEncoder().encode(data) {
|
|
14 UserDefaults.standard.set(userDataEncoded, forKey: "UserData")
|
9
|
15 }
|
|
16 }
|
|
17 }
|
14
|
18
|
9
|
19 init() {
|
14
|
20 if let userData = UserDefaults.standard.data(forKey: "UserData") {
|
19
|
21 if let decodedUserData = try? JSONDecoder().decode(UserDataModel.self, from: userData) {
|
14
|
22 data = decodedUserData
|
9
|
23 }
|
|
24 }
|
|
25 }
|
|
26 }
|