Swift之沙盒與數(shù)據(jù)存儲(chǔ)

應(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!)
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容