iOS應用程序只能在自己的目錄下進行文件的操作,不可以訪問其他的存儲空間,此區域被稱為沙盒。
應用沙盒結構分析
1、應用程序包:包含了所有的資源文件和可執行文件 2、Documents:保存應用運行時生成的需要持久化的數據,iTunes同步設備時會備份該目錄 3、tmp:保存應用運行時所需要的臨時數據,使用完畢后再將相應的文件從該目錄刪除。應用沒有運行,系統也可能會清除該目錄下的文件,iTunes不會同步備份該目錄 4、Library/Cache:保存應用運行時生成的需要持久化的數據,iTunes同步設備時不備份該目錄。一般存放體積大、不需要備份的非重要數據 5、Library/Preference:保存應用的所有偏好設置,IOS的Settings應用會在該目錄中查找應用的設置信息。iTunes
IOS中的數據存儲
/** 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
1、存儲為plist屬性列表
func saveWithFile() { // 1、獲得沙盒的根路徑 let home = NSHomeDirectory() as NSString // 2、獲得Documents路徑,使用NSString對象的stringByAppendingPathComponent()方法拼接路徑 let docPath = home.stringByAppendingPathComponent("Documents") as NSString // 3、獲取文本文件路徑 let filePath = docPath.stringByAppendingPathComponent("data.plist") let dataSource = NSMutableArray() dataSource.addObject("小橋上我曾背你走過") dataSource.addObject("河邊上為你放的煙火") dataSource.addObject("電影院最后一排座我們第一次吻過") dataSource.addObject("那么多浪漫我都記得") dataSource.addObject("別再跟着我漂泊") // 4、將數據寫入文件中 dataSource.writeToFile(filePath, atomically: true) } func readWithFile() { /// 1、獲得沙盒的根路徑 let home = NSHomeDirectory() as NSString /// 2、獲得Documents路徑,使用NSString對象的stringByAppendingPathComponent()方法拼接路徑 let docPath = home.stringByAppendingPathComponent("Documents") as NSString /// 3、獲取文本文件路徑 let filePath = docPath.stringByAppendingPathComponent("data.plist") let dataSource = NSArray(contentsOfFile: filePath) print(dataSource) }
2、使用NSUserDefaults存儲數據
func saveWithNSUserDefaults() { // 1、利用NSUserDefaults存儲數據 let defaults = NSUserDefaults.standardUserDefaults() // 2、存儲數據 defaults.setObject("衣帶漸寬終不悔", forKey: "name") // 3、同步數據 defaults.synchronize() } func readWithNSUserDefaults(){ let defaults = NSUserDefaults.standardUserDefaults() let name = defaults.stringForKey("name") let switch = defaults.boolForKey("bool") print(name) print(switch) }
3、歸檔存儲:對象需要實現NSCoding協議,歸檔對應encode,反歸檔對應decode
/** 歸檔數據 需要實現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!) }
4、SQlite3
5、CoreData
下面介紹常用的程序文件夾目錄:
1,Home目錄 ./
整個應用程序各文檔所在的目錄
|
1
2
|
//獲取程序的Home目錄
let homeDirectory = NSHomeDirectory()
|
2,Documnets目錄 ./Documents
用戶文檔目錄,蘋果建議將程序中建立的或在程序中瀏覽到的文件數據保存在該目錄下,iTunes備份和恢復的時候會包括此目錄
|
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"
|
3,Library目錄 ./Library
這個目錄下有兩個子目錄:Caches 和 Preferences
Library/Preferences目錄,包含應用程序的偏好設置文件。不應該直接創建偏好設置文件,而是應該使用NSUserDefaults類來取得和設置應用程序的偏好。
Library/Caches目錄,主要存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會再應用退出時刪除
|
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"
|
4,tmp目錄 ./tmp
用於存放臨時文件,保存應用程序再次啟動過程中不需要的信息,重啟后清空。
|
1
2
3
4
5
|
//方法1
let tmpDir = NSTemporaryDirectory()
//方法2
let tmpDir2 = NSHomeDirectory() + "/tmp"
|
5,程序打包安裝的目錄 NSBundle.mainBundle()
工程打包安裝后會在NSBundle.mainBundle()路徑下,該路徑是只讀的,不允許修改。
所以當我們工程中有一個SQLite數據庫要使用,在程序啟動時,我們可以把該路徑下的數據庫拷貝一份到Documents路徑下,以后整個工程都將操作Documents路徑下的數據庫。
|
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)
}
|
