KMAdvertisementCache.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. //2.利用NSSearchPathForDirectoriesInDomains获取路径
  38. //
  39. //
  40. }