Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录Library:存储程序的默认设置或其它状态信息;Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。Library/Preference:保存应用的所有偏好设置,IOS的Settings应用会在该目录中查找应用的设置信息。tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。
let home=NSHomeDirectory()//获取沙盒根路径
//document文档目录--方法一
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]
if urls.count > 0{
print("--------\(urls[0])")
print("--------\(urls)")
}
// 第四种获取路径方式
do{
let url = try fileManager.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)
print("--------\(url)")
}catch{
}
//library目录路径
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
// //临时目录路径temp
let tempPath = NSTemporaryDirectory() as NSString
==============Library/Preference:保存应用的所有偏好设置========
//偏好设置存储
let def=UserDefaults.standard
def.set("aa", forKey: "a")
def.synchronize()
//获取值
let str = def.string(forKey: "a")
=============吧数组存入.plist文件=从沙盒中读取文件=========
func saveWithFile() {
// 1、获得沙盒的根路径
let home=NSHomeDirectory()//获取沙盒根路径
//获取文档目录路径--方法二,沙盒根路径字符串拼接路径
let docPath = home+"/Documents/"
// 3、获取文本文件路径
let filePath = docPath + "data.plist"
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)
//归档
NSKeyedArchiver.archiveRootObject(archive, toFile: path)
}
func unarchive(){
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,否则找不到属性
print(archhive.name!)
print(archhive.age!)
} |