KMLightMemberCache.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // KMLightMemberCache.swift
  3. // KMLightMember
  4. //
  5. // Created by lizhe on 2022/11/25.
  6. //
  7. enum KMLightMemberCacheType: String {
  8. case info = "lightMemberInfoCache.plist"
  9. case token = "lightMemberTokenCache.plist"
  10. }
  11. class KMLightMemberCache: NSObject {
  12. let kFilePath: NSString = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.path + "KMLightMemberCache" as NSString
  13. //单例
  14. public static let cache = KMLightMemberCache()
  15. //存储
  16. func saveData(data: NSDictionary, type: KMLightMemberCacheType = .info) {
  17. let string: NSString = (kFilePath as String) + "/" + type.rawValue as NSString
  18. if (!FileManager.default.fileExists(atPath: string.deletingLastPathComponent as String)) {
  19. try?FileManager.default.createDirectory(atPath: string.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
  20. }
  21. if (!FileManager.default.fileExists(atPath: string as String)) {
  22. FileManager.default.createFile(atPath: string as String, contents: nil)
  23. }
  24. //获取有用的参数
  25. // let dic: NSMutableDictionary = [:]
  26. // for key in data.allKeys {
  27. // let object = data.object(forKey: key)
  28. // if let subDictionary = object as? [NSDictionary] {
  29. //
  30. // } else if (!(object is NSNull)) {
  31. // dic.setObject(object as Any, forKey: key as! NSCopying)
  32. // }
  33. // }
  34. // let dic: [String: Any] = data.allKeys // 待处理的字典数据
  35. let result = removeNullValuesFromDictionary(data as! [String : Any])
  36. // dic.removeObject(forKey: "subscriptionInfoList")
  37. let saveData: NSArray = [result]
  38. let success = saveData.write(toFile: string.expandingTildeInPath as String, atomically: true)
  39. if (success == true) {
  40. KMPrint("成功 -" + (string.expandingTildeInPath as String))
  41. } else {
  42. KMPrint("失败 -" + (string.expandingTildeInPath as String))
  43. }
  44. }
  45. //读取
  46. func readData( type: KMLightMemberCacheType = .info) -> NSDictionary {
  47. let string: NSString = (kFilePath as String) + "/" + type.rawValue as NSString
  48. let data = NSArray.init(contentsOfFile: string.expandingTildeInPath as String)
  49. if (data != nil) {
  50. return data!.firstObject as! NSDictionary
  51. } else {
  52. return [:]
  53. }
  54. }
  55. func clean() {
  56. if (FileManager.default.fileExists(atPath: kFilePath as String)) {
  57. try?FileManager.default.removeItem(atPath: kFilePath as String)
  58. }
  59. }
  60. }
  61. extension KMLightMemberCache {
  62. /**
  63. */
  64. func removeNullValuesFromDictionary(_ dictionary: [String: Any]) -> [String: Any] {
  65. var result: [String: Any] = [:]
  66. for (key, value) in dictionary {
  67. if let subDictionary = value as? [NSDictionary] {
  68. // 子集为数组,则递归遍历子字典并进行处理
  69. var array: [Any] = []
  70. for item in subDictionary {
  71. let cleanedSubDictionary = removeNullValuesFromDictionary(item as! [String : Any])
  72. array.append(cleanedSubDictionary)
  73. }
  74. if !array.isEmpty {
  75. result[key] = array
  76. }
  77. } else if let subDictionary = value as? [String: Any] {
  78. // 子集为字典,则递归遍历子字典并进行处理
  79. let cleanedSubDictionary = removeNullValuesFromDictionary(subDictionary)
  80. if !cleanedSubDictionary.isEmpty {
  81. result[key] = cleanedSubDictionary
  82. }
  83. } else if !(value is NSNull) {
  84. // 子集不为 null,则将键值对添加到结果字典中
  85. result[key] = value
  86. }
  87. // else if (value is NSNull) {
  88. // result[key] = ""
  89. // }
  90. }
  91. return result
  92. }
  93. }