-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserSettings.swift
32 lines (27 loc) · 1.01 KB
/
UserSettings.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
//
// UserSettings.swift
// Pocket Reader
//
// Created by Алексей Пархоменко on 17.04.2020.
// Copyright © 2020 Алексей Пархоменко. All rights reserved.
//
import UIKit
final class UserSettings {
private enum SettingKey: String {
case userBooks
}
static var userBooks: [BookItem] {
get {
guard let savedBooks = UserDefaults.standard.object(forKey: SettingKey.userBooks.rawValue) as? Data, let decodedBooks = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(savedBooks) as? [BookItem] else { return [] }
return decodedBooks
}
set {
let defaults = UserDefaults.standard
let key = SettingKey.userBooks.rawValue
if let savedData = try? NSKeyedArchiver.archivedData(withRootObject: newValue, requiringSecureCoding: false) {
print("value: \(newValue) was added to key \(key)")
defaults.set(savedData, forKey: key)
}
}
}
}