fix
无故事王国
2024-06-24 910e88b92778ae78f0444f5c6f6202668c3110f6
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
102
//
//  ArchiveTool.swift
//  DolphinEnglishLearnStudent
//
//  Created by 无故事王国 on 2024/6/21.
//
 
import Foundation
import HandyJSON
import JQTools
 
class ArchiveTool{
 
                /// 缓存数据
                /// - Parameters:
                ///   - model: HandJSON模型
                ///   - force: 是否强制写入
 
                @discardableResult
                static func archive(model:HandyJSON,force:Bool = false)->Bool{
                                guard let json = model.toJSON() else {return false}
                                guard let className = String(describing: model.self).components(separatedBy: "(").first else {return false}
                                guard var filePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first else {return false}
                                filePath =  filePath + "/archive"
 
                                if FileManager.default.fileExists(atPath: filePath) && !force{return true}
 
                                do {
                                                try FileManager.default.createDirectory(at:URL(fileURLWithPath: filePath), withIntermediateDirectories: true)
                                }catch {
                                                return false
                                }
                                filePath = filePath +    "/\(className.jq_md5String()).archive"
 
                                guard let data = try? NSKeyedArchiver.archivedData(withRootObject: json, requiringSecureCoding: true) else {return false}
                                do{
                                                try data.write(to: URL(fileURLWithPath: filePath))
                                                return true
                                }catch _  {
                                                return false
                                }
                }
 
                static func archive(models:[HandyJSON],force:Bool = false)->Bool{
                                let jsonString = models.jq_toJson1()
                                guard let className = String(describing: models.self).components(separatedBy: "(").first else {return false}
                                guard var filePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first else {return false}
                                filePath =  filePath + "/archive"
 
                                if FileManager.default.fileExists(atPath: filePath) && !force{return true}
 
                                do {
                                                try FileManager.default.createDirectory(at:URL(fileURLWithPath: filePath), withIntermediateDirectories: true)
                                }catch {
                                                return false
                                }
                                filePath = filePath +    "/\(className.jq_md5String()).archive"
 
                                guard let data = try? NSKeyedArchiver.archivedData(withRootObject: jsonString, requiringSecureCoding: true) else {return false}
                                do{
                                                try data.write(to: URL(fileURLWithPath: filePath))
                                                return true
                                }catch _  {
                                                return false
                                }
                }
 
                static func unarchive(model:HandyJSON.Type)->HandyJSON?{
                                guard let className = String(describing: model.self).components(separatedBy: "(").first else {return nil}
                                let encryptName = className.jq_md5String()
 
                                guard var filePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first else {return nil}
                                filePath =  filePath + "/archive/" + encryptName + ".archive"
 
                                let data = try? Data(contentsOf: URL(fileURLWithPath: filePath))
 
                                if let dict = NSKeyedUnarchiver.unarchiveObject(with: data!) as? Dictionary<String, Any>{
                                                print(dict)
                                                let model = model.deserialize(from: dict)
                                                return model
                                }
                                return nil
                }
 
                static func unarchive(models:[HandyJSON].Type)->[HandyJSON]?{
                                guard let className = String(describing: models.self).components(separatedBy: "(").first else {return nil}
                                let encryptName = className.jq_md5String()
 
                                guard var filePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first else {return nil}
                                filePath =  filePath + "/archive/" + encryptName + ".archive"
 
                                let data = try? Data(contentsOf: URL(fileURLWithPath: filePath))
 
                                if let dict = NSKeyedUnarchiver.unarchiveObject(with: data!) as? Dictionary<String, Any>{
                                                print(dict)
                                                let model = models.deserialize(from: dict)
                                                return model
                                }
                                return nil
                }
 
}