KMAdvertisementCache.swift 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // KMAdvertisementCache.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/25.
  6. //
  7. class KMAdvertisementCache: NSObject {
  8. let kFilePath: NSString = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, false).first?.appending("/advertisement.plist"))! as NSString
  9. //单例
  10. public static let `default` = KMAdvertisementCache()
  11. //存储
  12. func saveData(data: [NSDictionary]) {
  13. if (FileManager.default.fileExists(atPath: kFilePath as String)) {
  14. FileManager.default.createFile(atPath: kFilePath as String, contents: nil);
  15. }
  16. let saveData: NSArray = data as NSArray
  17. let success = saveData.write(toFile: kFilePath.expandingTildeInPath as String, atomically: true)
  18. if (success == true) {
  19. print("成功 -" + (kFilePath.expandingTildeInPath as String))
  20. } else {
  21. print("失败 -" + (kFilePath.expandingTildeInPath as String))
  22. }
  23. }
  24. //读取
  25. func readData() -> [NSDictionary] {
  26. let data = NSArray.init(contentsOfFile: kFilePath.expandingTildeInPath as String)
  27. return data as! [NSDictionary]
  28. }
  29. //2.利用NSSearchPathForDirectoriesInDomains获取路径
  30. //
  31. //
  32. }