KMDataManager.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // KMDataManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/2/23.
  6. //
  7. import Cocoa
  8. // 数据持久化 【UserDefault、本地存储、数据库】
  9. let kDocumentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  10. let kAppSupportDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
  11. let kAppSupportOfBundleIdentifierDirectory = kAppSupportDirectory.appendingPathComponent(MainBundle.bundleIdentifier ?? kBundleIdentifier)
  12. class KMDataManager: NSObject {
  13. // 单例
  14. static let `default` = KMDataManager()
  15. var isTabbingWin = false //多页签提示页是否展示中
  16. }
  17. // MARK: - Paths
  18. extension KMDataManager {
  19. class func fetchAppSupportOfBundleIdentifierDirectory(makeIfNecessary: Bool = true) -> URL {
  20. let url = kAppSupportOfBundleIdentifierDirectory
  21. if makeIfNecessary {
  22. if FileManager.default.fileExists(atPath: url.path) == false {
  23. try?FileManager.default.createDirectory(at: url, withIntermediateDirectories: false)
  24. }
  25. }
  26. return url
  27. }
  28. }
  29. // MARK: - UserDefault
  30. extension KMDataManager {
  31. class func ud_object(forKey defaultName: String) -> Any? {
  32. return self.userD_.object(forKey: defaultName)
  33. }
  34. class func ud_set(_ value: Any?, forKey defaultName: String, sync: Bool = true) {
  35. self.userD_.set(value, forKey: defaultName)
  36. if sync {
  37. self.userD_.synchronize()
  38. }
  39. }
  40. class func ud_removeObject(forKey defaultName: String, sync: Bool = true) {
  41. self.userD_.removeObject(forKey: defaultName)
  42. }
  43. class func ud_string(forKey defaultName: String) -> String? {
  44. return self.userD_.string(forKey: defaultName)
  45. }
  46. class func ud_array(forKey defaultName: String) -> [Any]? {
  47. return self.userD_.array(forKey: defaultName)
  48. }
  49. class func ud_dictionary(forKey defaultName: String) -> [String : Any]? {
  50. return self.userD_.dictionary(forKey: defaultName)
  51. }
  52. class func ud_data(forKey defaultName: String) -> Data? {
  53. return self.userD_.data(forKey: defaultName)
  54. }
  55. class func ud_stringArray(forKey defaultName: String) -> [String]? {
  56. return self.userD_.stringArray(forKey: defaultName)
  57. }
  58. class func ud_integer(forKey defaultName: String) -> Int {
  59. return self.userD_.integer(forKey: defaultName)
  60. }
  61. class func ud_float(forKey defaultName: String) -> Float {
  62. return self.userD_.float(forKey: defaultName)
  63. }
  64. class func ud_double(forKey defaultName: String) -> Double {
  65. return self.userD_.double(forKey: defaultName)
  66. }
  67. class func ud_bool(forKey defaultName: String) -> Bool {
  68. return self.userD_.bool(forKey: defaultName)
  69. }
  70. class func ud_url(forKey defaultName: String) -> URL? {
  71. return self.userD_.url(forKey: defaultName)
  72. }
  73. class func ud_set(_ value: Int, forKey defaultName: String, sync: Bool = true) {
  74. self.userD_.set(value, forKey: defaultName)
  75. if sync {
  76. self.userD_.synchronize()
  77. }
  78. }
  79. class func ud_set(_ value: Float, forKey defaultName: String, sync: Bool = true) {
  80. self.userD_.set(value, forKey: defaultName)
  81. if sync {
  82. self.userD_.synchronize()
  83. }
  84. }
  85. class func ud_set(_ value: Double, forKey defaultName: String, sync: Bool = true) {
  86. self.userD_.set(value, forKey: defaultName)
  87. if sync {
  88. self.userD_.synchronize()
  89. }
  90. }
  91. class func ud_set(_ value: Bool, forKey defaultName: String, sync: Bool = true) {
  92. self.userD_.set(value, forKey: defaultName)
  93. if sync {
  94. self.userD_.synchronize()
  95. }
  96. }
  97. class func ud_set(_ url: URL?, forKey defaultName: String, sync: Bool = true) {
  98. self.userD_.set(url, forKey: defaultName)
  99. if sync {
  100. self.userD_.synchronize()
  101. }
  102. }
  103. // 私有
  104. private class var userD_: UserDefaults {
  105. get {
  106. return UserDefaults.standard
  107. }
  108. }
  109. }