KMAdvertisementCache.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // KMAdvertisementCache.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/25.
  6. //
  7. class KMAdvertisementCache: NSObject {
  8. let kFilePath: NSString = NSTemporaryDirectory() + "Advertisement/advertisement.plist" as NSString
  9. //单例
  10. public static let `default` = KMAdvertisementCache()
  11. //存储
  12. func saveData(data: [NSDictionary]) {
  13. if (!FileManager.default.fileExists(atPath: kFilePath.deletingLastPathComponent as String)) {
  14. try?FileManager.default.createDirectory(atPath: kFilePath.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
  15. }
  16. if (!FileManager.default.fileExists(atPath: kFilePath as String)) {
  17. FileManager.default.createFile(atPath: kFilePath as String, contents: nil);
  18. }
  19. let saveData: NSArray = data as NSArray
  20. let success = saveData.write(toFile: kFilePath.expandingTildeInPath as String, atomically: true)
  21. if (success == true) {
  22. print("成功 -" + (kFilePath.expandingTildeInPath as String))
  23. } else {
  24. print("失败 -" + (kFilePath.expandingTildeInPath as String))
  25. }
  26. }
  27. //读取
  28. func readData() -> [NSDictionary] {
  29. print("数据地址 = " + (kFilePath.expandingTildeInPath as String))
  30. let data = NSArray.init(contentsOfFile: kFilePath.expandingTildeInPath as String)
  31. if (data != nil) {
  32. return data as! [NSDictionary]
  33. } else {
  34. return []
  35. }
  36. }
  37. }