一、应用程序包:包含了全部的资源文件和可执行文件 二、Documents:保存应用运行时生成的须要持久化的数据,iTunes同步设备时会备份该目录 三、tmp:保存应用运行时所须要的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行,系统也可能会清除该目录下的文件,iTunes不会同步备份该目录 四、Library/Cache:保存应用运行时生成的须要持久化的数据,iTunes同步设备时不备份该目录。通常存放体积大、不须要备份的非重要数据 五、Library/Preference:保存应用的全部偏好设置,IOS的Settings应用会在该目录中查找应用的设置信息。iTunes
一、应用程序包:包含了全部的资源文件和可执行文件 二、Documents:保存应用运行时生成的须要持久化的数据,iTunes同步设备时会备份该目录 三、tmp:保存应用运行时所须要的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行,系统也可能会清除该目录下的文件,iTunes不会同步备份该目录 四、Library/Cache:保存应用运行时生成的须要持久化的数据,iTunes同步设备时不备份该目录。通常存放体积大、不须要备份的非重要数据 五、Library/Preference:保存应用的全部偏好设置,IOS的Settings应用会在该目录中查找应用的设置信息。iTunes
/** NSSearchPathDirectory.DocumentDirectory 查找Documents文件夹 NSSearchPathDomainMask.UserDomainMask 在用户的应用程序下查找 true 展开路径 false 当前应用的根路径 == “~” */ let docPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as NSString // 上面代码代替下面代码,防止Documen文件夹不存在***************************************************************** // 得到沙盒的根路径 let home = NSHomeDirectory() as NSString // 得到Documents路径,使用NSString对象的stringByAppendingPathComponent()方法拼接路径 let docPath = home.stringByAppendingPathComponent("Documents") as NSString
一、存储为plist属性列表sql
func saveWithFile() { // 一、得到沙盒的根路径 let home = NSHomeDirectory() as NSString // 二、得到Documents路径,使用NSString对象的stringByAppendingPathComponent()方法拼接路径 let docPath = home.stringByAppendingPathComponent("Documents") as NSString // 三、获取文本文件路径 let filePath = docPath.stringByAppendingPathComponent("data.plist") let dataSource = NSMutableArray() dataSource.addObject("小桥上我曾背你走过") dataSource.addObject("河边上为你放的烟火") dataSource.addObject("电影院最后一排座咱们第一次吻过") dataSource.addObject("那么多浪漫我都记得") dataSource.addObject("别再跟着我漂泊") // 四、将数据写入文件中 dataSource.writeToFile(filePath, atomically: true) } func readWithFile() { /// 一、得到沙盒的根路径 let home = NSHomeDirectory() as NSString /// 二、得到Documents路径,使用NSString对象的stringByAppendingPathComponent()方法拼接路径 let docPath = home.stringByAppendingPathComponent("Documents") as NSString /// 三、获取文本文件路径 let filePath = docPath.stringByAppendingPathComponent("data.plist") let dataSource = NSArray(contentsOfFile: filePath) print(dataSource) }
二、使用NSUserDefaults存储数据数据库
func saveWithNSUserDefaults() { // 一、利用NSUserDefaults存储数据 let defaults = NSUserDefaults.standardUserDefaults() // 二、存储数据 defaults.setObject("衣带渐宽终不悔", forKey: "name") // 三、同步数据 defaults.synchronize() } func readWithNSUserDefaults(){ let defaults = NSUserDefaults.standardUserDefaults() let name = defaults.stringForKey("name") let switch = defaults.boolForKey("bool") print(name) print(switch) }
三、归档存储:对象须要实现NSCoding协议,归档对应encode,反归档对应decodeswift
/** 归档数据 须要实现NSCoding协议 */ func saveWithNSKeyedArchiver() { let docPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as NSString let filePath = docPath.stringByAppendingPathComponent("contact.data") let contact = Contact(name: "123333", phone: "123456") /** * 数据归档处理 */ NSKeyedArchiver.archiveRootObject(contact, toFile: filePath) } // 若是上面直接运行会报错,由于你须要在要归档的对象中遵循NSCoding协议,并实现归档方法和解析方法 如: class Contact: NSObject, NSCoding { var name: String? var phone: String? required init(name: String, phone: String){ self.name = name self.phone = phone } // 在对象归档的时候调用(哪些属性须要归档,怎么归档) func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeObject(name, forKey: "name") } // 解析NIB/XIB的时候会调用 required init?(coder aDecoder: NSCoder) { super.init() name = aDecoder.decodeObjectForKey("name") as? String } } /** 反归档数据 */ func readWithNSKeyedUnarchiver() { let docPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as NSString let filePath = docPath.stringByAppendingPathComponent("contact.data") let contact = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! Contact print(contact.name!) }
四、SQlite3缓存
五、CoreDataui
1
2
|
//获取程序的Home目录
let homeDirectory = NSHomeDirectory ()
|
1
2
3
4
5
6
7
|
//方法1
let documentPaths = NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory . DocumentDirectory ,
NSSearchPathDomainMask . UserDomainMask , true )
let documnetPath = documentPaths[0] as ! String
//方法2
let ducumentPath2 = NSHomeDirectory () + "/Documents"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//Library目录-方法1
let libraryPaths = NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory . LibraryDirectory ,
NSSearchPathDomainMask . UserDomainMask , true )
let libraryPath = libraryPaths[0] as ! String
//Library目录-方法2
let libraryPath2 = NSHomeDirectory () + "/Library"
//Cache目录-方法1
let cachePaths = NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory . CachesDirectory ,
NSSearchPathDomainMask . UserDomainMask , true )
let cachePath = cachePaths[0] as ! String
//Cache目录-方法2
let cachePath2 = NSHomeDirectory () + "/Library/Caches"
|
1
2
3
4
5
|
//方法1
let tmpDir = NSTemporaryDirectory ()
//方法2
let tmpDir2 = NSHomeDirectory () + "/tmp"
|
1
2
3
4
5
6
7
8
9
|
//声明一个Documents下的路径
var dbPath = NSHomeDirectory () + "/Documents/hanggeDB.sqlite"
//判断数据库文件是否存在
if ! NSFileManager .defaultManager().fileExistsAtPath(dbPath){
//获取安装包内数据库路径
var bundleDBPath: String ? = NSBundle .mainBundle().pathForResource( "hanggeDB" , ofType: "sqlite" )
//将安装包内数据库拷贝到Documents目录下
NSFileManager .defaultManager().copyItemAtPath(bundleDBPath!, toPath: dbPath, error: nil )
}
|