應(yīng)用沙盒結(jié)構(gòu)分析
1、應(yīng)用程序包:包含了所有的資源文件和可執(zhí)行文件
2、Documents:保存應(yīng)用運(yùn)行時(shí)生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時(shí)會(huì)備份該目錄
3、tmp:保存應(yīng)用運(yùn)行時(shí)所需要的臨時(shí)數(shù)據(jù),使用完畢后再將相應(yīng)的文件從該目錄刪除。應(yīng)用沒有運(yùn)行,系統(tǒng)也可能會(huì)清除該目錄下的文件,iTunes不會(huì)同步備份該目錄
4、Library/Cache:保存應(yīng)用運(yùn)行時(shí)生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時(shí)不備份該目錄。一般存放體積大、不需要備份的非重要數(shù)據(jù)
5、Library/Preference:保存應(yīng)用的所有偏好設(shè)置,IOS的Settings應(yīng)用會(huì)在該目錄中查找應(yīng)用的設(shè)置信息。iTunes同步設(shè)備時(shí)會(huì)備份該目錄
IOS中的數(shù)據(jù)存儲(chǔ)
NSSearchPathDirectory.DocumentDirectory 查找Documents文件夾
NSSearchPathDomainMask.UserDomainMask 在用戶的應(yīng)用程序下查找true 展開路徑 false 當(dāng)前應(yīng)用的根路徑 == “~”
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、存儲(chǔ)為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、將數(shù)據(jù)寫入文件中
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存儲(chǔ)數(shù)據(jù)
func saveWithNSUserDefaults() {
// 1、利用NSUserDefaults存儲(chǔ)數(shù)據(jù)
let defaults = NSUserDefaults.standardUserDefaults();
// 2、存儲(chǔ)數(shù)據(jù) defaults.setObject("衣帶漸寬終不悔", forKey: "name");
// 3、同步數(shù)據(jù) defaults.synchronize();
}
func readWithNSUserDefaults(){
let defaults = NSUserDefaults.standardUserDefaults();
let name = defaults.stringForKey("name")
let switch = defaults.boolForKey("bool")
print(name) print(switch)
}
3、歸檔存儲(chǔ):對象需要實(shí)現(xiàn)NSCoding協(xié)議,歸檔對應(yīng)encode,反歸檔對應(yīng)decode
/** 歸檔數(shù)據(jù) 需要實(shí)現(xiàn)NSCoding協(xié)議 */
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")
/** * 數(shù)據(jù)歸檔處理 */
NSKeyedArchiver.archiveRootObject(contact, toFile: filePath);
}
// 如果上面直接運(yùn)行會(huì)報(bào)錯(cuò),因?yàn)槟阈枰谝獨(dú)w檔的對象中遵循NSCoding協(xié)議,并實(shí)現(xiàn)歸檔方法和解析方法 如:
class Contact: NSObject, NSCoding {
var name: String?
var phone: String?
required init(name: String, phone: String){
self.name = name
self.phone = phone
}
// 在對象歸檔的時(shí)候調(diào)用(哪些屬性需要?dú)w檔,怎么歸檔)
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
}
// 解析NIB/XIB的時(shí)候會(huì)調(diào)用
required init?(coder aDecoder: NSCoder) {
super.init()
name = aDecoder.decodeObjectForKey("name") as? String
}
}
/** 反歸檔數(shù)據(jù) */
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!)
}