// //字符串
// let name = "伟伟你好"
// let document = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
// print(document[0])
// //创建文件路径
// let path = document[0] + "/index.txt"
// //创建文件 文件内容
// let fileM = FileManager()
// fileM.createFile(atPath: path, contents: nil, attributes: nil)
// print(path)
// //写入文件
// try! name.write(toFile: path, atomically: true, encoding: .utf8)
// //移动文件 文件夹只能移文件夹 文件只能移文件
// let tmp = NSTemporaryDirectory() + "index.txt"
// try! fileM.moveItem(atPath: path, toPath: tmp)
// //try! fileM.removeItem(atPath: tmp)
//数组
// let array = [1,2,3,2,4]
// //创建路径
// let document = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "arr.plist"
// //创建文件
// let file = FileManager()
// file.createFile(atPath: document, contents: nil, attributes: nil)
// let url = URL(fileURLWithPath: document)
// (array as NSArray).write(to: url, atomically: true)
// //读取
// let arr1 = NSArray(contentsOfFile: document)
// print(arr1!)
//字典
let dic = ["A":"Apple","B":"Bubble"]
//创建路径
let document = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "dic.plist"
//创建文件
let file = FileManager()
file.createFile(atPath: document, contents: nil, attributes: nil)
let url = URL(fileURLWithPath: document)
(dic as NSDictionary).write(to: url, atomically: true)
//读取
let arr1 = NSDictionary(contentsOfFile: document)
print(arr1!)
网友评论