CNLiveFileManager.swift
2.95 KB
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
//
// CNLiveFileManager.swift
// metaCode
//
// Created by open on 2023/11/13.
//
import Foundation
class CNLiveFileManager: NSObject {
static let shared = CNLiveFileManager.init()
open func save(object: Any, key: String) {
var temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
if (temp_dic == nil) {
temp_dic = NSMutableDictionary.init()
}
temp_dic?.setObject(object, forKey: key as NSCopying)
temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
}
open func take(key: String) -> Any {
let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
if (temp_dic == nil) {
return NSMutableDictionary()
}
return temp_dic?.object(forKey: key) as Any
}
open func takeAll() -> NSDictionary {
let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
if (temp_dic == nil) {
return NSMutableDictionary()
}
return temp_dic ?? NSDictionary()
}
open func resetAllSave() {
let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
if temp_dic != nil {
temp_dic?.removeAllObjects()
temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
}
}
open func delete(key: String) {
let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
if temp_dic != nil {
temp_dic?.removeObject(forKey: key)
temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
}
}
private lazy var userFileDir: String = {
let userFileDir = NSString()
var path: String = ""
path = "\(fileDir)/userId_\(UserInfoManager.shared.userInfo.uid)/"
let fileManager = FileManager.default
var isDir: ObjCBool = false
let exist: Bool = fileManager.fileExists(atPath: path, isDirectory: &isDir)
if !(isDir.boolValue && exist) {
do {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
} catch {
debugPrint("文件目录创建失败")
return ""
}
}
return path as String
}()
private lazy var fileDir: String = {
let path = "\(NSHomeDirectory())/Documents/metaCode"
let fileManager = FileManager.default
var isDir: ObjCBool = false
let exist: Bool = fileManager.fileExists(atPath: path, isDirectory: &isDir)
if !(isDir.boolValue && exist) {
do {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
} catch {
debugPrint("文件目录创建失败")
return ""
}
}
return path as String
}()
}