天天看點

iOS筆記—沙盒(sandbox)

當ios app應用被建立時,就會生成一個專有的沙盒,每個沙盒中包含有三個檔案,分别是:Documents、Library和tmp。顧名思義,這三個檔案夾的作用分别存放檔案類的檔案、存放庫類的系統狀态檔案和臨時存放的檔案。

Document:一般存放需要持久的資料,iTunes備份和恢複會把該資料還原。(可在此目錄中添加子檔案夾友善資料分類)。該目錄是用來備份資料的,如果該檔案夾備份内容過大會不能通過Apple稽核。

Library/Caches:設定程式的預設設定和其他狀态資訊。存放緩存檔案,iTunes不會備份此目錄。此目錄應用退出後不會被删除。(資料需要長時間使用,又不需要備份的,将其放入Caches目錄)

Tmp:臨時檔案的目錄,存放臨時,iTunes不會備份此目錄,此目錄可能在應用退出後被删除。(在記憶體吃緊的時候,tmp檔案夾的内容會自動清除)

// 沙盒的主目錄
        NSString *homePath = NSHomeDirectory();
        NSLog(@"homepath is = %@",homePath);
        
        // 沙盒下的Documents目錄
        NSString *documentsPath = [homePath stringByAppendingPathComponent:@"Documents"];
        NSLog(@"documentspath is = %@",documentsPath);
        
        
        // 沙盒下的Library目錄
        NSString *libraryPath = [homePath stringByAppendingPathComponent:@"Library"];
        
        NSLog(@"librarypath is %@", libraryPath);
        
        // 沙盒下的tmp目錄
        NSString *tmpPath = [homePath stringByAppendingPathComponent:@"tmp"];
        NSLog(@"tmp is %@", tmpPath);
        
        // 擷取檔案路徑的組成部分
        NSArray *components = [tmpPath pathComponents];
        NSLog(@"components is = %@", components);
        
        // 擷取檔案的最後組成部分
        NSString *lastName = [tmpPath lastPathComponent];
        NSLog(@"lastName is = %@", lastName);
        
        // 檔案路徑的追加
        NSString *superaddtion = [tmpPath stringByAppendingPathComponent:@"text.png"];
        NSLog(@"superaddtion is = %@",superaddtion);
        
        // 檔案路徑删除最後組成部分
        NSString *deleteLast =[tmpPath stringByDeletingLastPathComponent];
        NSLog(@"deleteLast is = %@",deleteLast);
        
        // 追加擴充名
        NSString *extensionName = [tmpPath stringByAppendingPathExtension:@"txt"];
        NSLog(@"extensionName is = %@", extensionName);
        
        // 檔案路徑最後部分的擴充名 傳回值為字元串
        NSString * exten = [tmpPath pathExtension];
        NSLog(@" 擴充名 is = %@", exten);