//
//  KMAdvertisementCache.swift
//  KMAdvertisement
//
//  Created by lizhe on 2022/11/25.
//

class KMAdvertisementCache: NSObject {
    let kFilePath: NSString = NSTemporaryDirectory() + "Advertisement/advertisement.plist" as NSString
    
    //单例
    public static let `default` = KMAdvertisementCache()
    //存储
    func saveData(data: [NSDictionary]) {
        if (!FileManager.default.fileExists(atPath: kFilePath.deletingLastPathComponent as String)) {
            try?FileManager.default.createDirectory(atPath: kFilePath.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
        }
        
        if (!FileManager.default.fileExists(atPath: kFilePath as String)) {
            FileManager.default.createFile(atPath: kFilePath as String, contents: nil);
        }
        
        let saveData: NSArray = data as NSArray
        let success = saveData.write(toFile: kFilePath.expandingTildeInPath as String, atomically: true)

        if (success == true) {
            print("成功 -" + (kFilePath.expandingTildeInPath as String))
        } else {
            print("失败 -" + (kFilePath.expandingTildeInPath as String))
        }
    }

   //读取
    func readData() -> [NSDictionary] {
        print("数据地址 = " + (kFilePath.expandingTildeInPath as String))
        let data =  NSArray.init(contentsOfFile: kFilePath.expandingTildeInPath as String)
        if (data != nil) {
            return data as! [NSDictionary]
        } else {
            return []
        }
    }

//2.利用NSSearchPathForDirectoriesInDomains获取路径
//
//
}
