|
@@ -212,3 +212,50 @@ extension KMDataManager {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// MARK: - UserDefault 的补充
|
|
|
+
|
|
|
+extension KMDataManager {
|
|
|
+ private static let userD_dirname_key_ = "KMUserDefaultExtension"
|
|
|
+ private static let userD_filename_key_ = "KMUserDefaultExtension.plist"
|
|
|
+
|
|
|
+ // 保存数据 [清空数据时传nil]
|
|
|
+ class func udExtension_set(_ value: Any?, forKey defaultName: String) {
|
|
|
+ // 存储文件路径
|
|
|
+ let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
|
|
|
+ if FileManager.default.fileExists(atPath: dir.path) == false {
|
|
|
+ try?FileManager.default.createDirectory(at: dir, withIntermediateDirectories: false)
|
|
|
+ }
|
|
|
+ // 存储文件名称
|
|
|
+ let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
|
|
|
+ if FileManager.default.fileExists(atPath: fileUrl.path) == false {
|
|
|
+ try?FileManager.default.createFile(atPath: fileUrl.path, contents: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取原始文件数据
|
|
|
+ var info = NSMutableDictionary()
|
|
|
+ if let data = try?NSDictionary(contentsOf: fileUrl, error: ()) {
|
|
|
+ info.addEntries(from: data as? [AnyHashable : Any] ?? [:])
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存数据
|
|
|
+ info.setObject(value as Any, forKey: defaultName as NSCopying)
|
|
|
+ info.write(to: fileUrl, atomically: true)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取数据
|
|
|
+ class func udExtension_object(forKey defaultName: String) -> Any? {
|
|
|
+ // 查找文件
|
|
|
+ let dir = self.fetchAppSupportOfBundleIdentifierDirectory().appendingPathComponent(self.userD_dirname_key_)
|
|
|
+ let fileUrl = dir.appendingPathComponent(self.userD_filename_key_)
|
|
|
+ guard let dict = NSDictionary(contentsOfFile: fileUrl.path) else {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ // 获取数据
|
|
|
+ if dict.count == 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ return dict[defaultName]
|
|
|
+ }
|
|
|
+}
|