// // KMDataManager.swift // PDF Reader Pro // // Created by tangchao on 2024/2/23. // import Cocoa // 数据持久化 【UserDefault、本地存储、数据库】 let kDocumentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let kAppSupportDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first! let kAppSupportOfBundleIdentifierDirectory = kAppSupportDirectory.appendingPathComponent(MainBundle.bundleIdentifier ?? kBundleIdentifier) class KMDataManager: NSObject { // 单例 static let `default` = KMDataManager() var isTabbingWin = false //多页签提示页是否展示中 } // MARK: - Paths extension KMDataManager { class func fetchAppSupportOfBundleIdentifierDirectory(makeIfNecessary: Bool = true) -> URL { let url = kAppSupportOfBundleIdentifierDirectory if makeIfNecessary { if FileManager.default.fileExists(atPath: url.path) == false { try?FileManager.default.createDirectory(at: url, withIntermediateDirectories: false) } } return url } } // MARK: - UserDefault extension KMDataManager { class func ud_object(forKey defaultName: String) -> Any? { return self.userD_.object(forKey: defaultName) } class func ud_set(_ value: Any?, forKey defaultName: String, sync: Bool = true) { self.userD_.set(value, forKey: defaultName) if sync { self.userD_.synchronize() } } class func ud_removeObject(forKey defaultName: String, sync: Bool = true) { self.userD_.removeObject(forKey: defaultName) } class func ud_string(forKey defaultName: String) -> String? { return self.userD_.string(forKey: defaultName) } class func ud_array(forKey defaultName: String) -> [Any]? { return self.userD_.array(forKey: defaultName) } class func ud_dictionary(forKey defaultName: String) -> [String : Any]? { return self.userD_.dictionary(forKey: defaultName) } class func ud_data(forKey defaultName: String) -> Data? { return self.userD_.data(forKey: defaultName) } class func ud_stringArray(forKey defaultName: String) -> [String]? { return self.userD_.stringArray(forKey: defaultName) } class func ud_integer(forKey defaultName: String) -> Int { return self.userD_.integer(forKey: defaultName) } class func ud_float(forKey defaultName: String) -> Float { return self.userD_.float(forKey: defaultName) } class func ud_double(forKey defaultName: String) -> Double { return self.userD_.double(forKey: defaultName) } class func ud_bool(forKey defaultName: String) -> Bool { return self.userD_.bool(forKey: defaultName) } class func ud_url(forKey defaultName: String) -> URL? { return self.userD_.url(forKey: defaultName) } class func ud_set(_ value: Int, forKey defaultName: String, sync: Bool = true) { self.userD_.set(value, forKey: defaultName) if sync { self.userD_.synchronize() } } class func ud_set(_ value: Float, forKey defaultName: String, sync: Bool = true) { self.userD_.set(value, forKey: defaultName) if sync { self.userD_.synchronize() } } class func ud_set(_ value: Double, forKey defaultName: String, sync: Bool = true) { self.userD_.set(value, forKey: defaultName) if sync { self.userD_.synchronize() } } class func ud_set(_ value: Bool, forKey defaultName: String, sync: Bool = true) { self.userD_.set(value, forKey: defaultName) if sync { self.userD_.synchronize() } } class func ud_set(_ url: URL?, forKey defaultName: String, sync: Bool = true) { self.userD_.set(url, forKey: defaultName) if sync { self.userD_.synchronize() } } // 私有 private class var userD_: UserDefaults { get { return UserDefaults.standard } } }