let documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true).last
//获取文档目录路径--方法二,沙盒根路径字符串拼接路径
let docPath = home+"/Documents/"
let fileManager = FileManager.default
let fileManager2 = FileManager()
// 第三种获取路径方式
let urls = fileManager.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask) as [NSURL]
let libtraryPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as String
//缓存目录路径cache
let cachePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString
let ar=["aa","ba","ca"] as NSArray//必须要转换成NSArray,否则不成功报错
// 4、将数据写入文件中
ar.write(toFile: filePath,atomically : true)
}
func readWithFile() {
// 1、获得沙盒的根路径
let home=NSHomeDirectory()//获取沙盒根路径
//获取文档目录路径--方法二,沙盒根路径字符串拼接路径
let docPath = home+"/Documents/"
// 3、获取文本文件路径
let filePath = docPath + "data.plist"
let ar=NSArray.init(contentsOfFile: filePath)
print(ar)
}
============================归档存储,获取数据======
import UIKit
class LYBARCHIVE: NSObject,NSCoding {
var name:String?
var age:Int?
init(name:String,age:Int){
self.name=name
self.age=age
}
// 在对象归档的时候调用(哪些属性需要归档,怎么归档)
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(age, forKey: "age")
}
// 解析NIB/XIB的时候会调用
required init?(coder aDecoder: NSCoder) {
super.init()
name = aDecoder.decodeObject(forKey: "name") as? String
age=aDecoder.decodeObject(forKey: "age") as? Int
}
}
**********
func archive(){
let documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true).last
let path=documentPath! + "a.data"
print(path)
let archive = LYBARCHIVE.init(name: "lambo", age: 30)
let documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true).last
let path=documentPath! + "a.data"
//解归档
let archhive=NSKeyedUnarchiver.unarchiveObject(withFile: path)as!LYBARCHIVE//这里一定要请转成LYBARCHIVE,否则找不到属性