一、應用程序包:包含了全部的資源文件和可執行文件 二、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 )
}
|