//存储nsdata
class func savejson(dic:NSData) {
let filePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true);
let libr = filePath[0]+"loging"
var bol = Bool()
bol = dic.write(toFile: libr, atomically: true) //存储不同类型 有不同方法
if bol {
print("储储成功")
}else{
print("储储失败")
}
}
//读取
class func readloginData(){
do{
let filePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true);
let libr = filePath[0]+"loging"
let fileManager = FileManager.default
let exist = fileManager.fileExists(atPath: libr)
if exist{
let content = fileManager.contents(atPath: libr)
let dat:Data = (content)!
let dict = String(data: dat, encoding: String.Encoding.utf8)
let jsonData = dict?.data(using: String.Encoding.utf8, allowLossyConversion: false) ?? Data()
guard let json = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)else{
return
}
print(json)
}
}
}
网友评论