swiftデバイスストレージ、読み取り
1295 ワード
//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:true:true:true:true//異なるタイプのストレージには異なるメソッドif bol{print("ストレージ成功")}else{print("ストレージ失敗")があります. } }
//class func readloginData(){
//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)
}
}
}