国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS文件使用總結(jié)(數(shù)據(jù)持久化存儲(chǔ))

2019-11-08 00:23:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一. iOS沙盒存儲(chǔ)結(jié)構(gòu)

 沙盒目錄是一種數(shù)據(jù)安全策略,設(shè)計(jì)原理是只允許自己的應(yīng)用訪問(wèn)目錄,而不允許其他應(yīng)用訪問(wèn),只有特定的API共享特定的數(shù)據(jù)。目錄結(jié)構(gòu)如下:

|---------------Documents: 用于用戶生成的大文件或需要非常頻繁更新的數(shù)據(jù),能夠進(jìn)行iTunes或iCloud備份。(游戲進(jìn)度,寫(xiě)的日記什么的)

|            /----------List.sqlite3

|---------------Library:

|           /----------Caches  用于存放網(wǎng)絡(luò)獲取的數(shù)據(jù)。(音樂(lè)圖片緩存)(不會(huì)iCloud備份)

|           /----------PReferences  用戶偏好設(shè)置(iCloud備份)NSUserDefault

|---------------tmp: 臨時(shí)存儲(chǔ)。(手機(jī)存儲(chǔ)不夠系統(tǒng)會(huì)去刪除里面的東西)

|---------------YourApp.app:  bundle包。(程序包資源)

目錄獲取方式:

1.Mac模擬器沙盒總路徑:

NSString *homePath = NSHomeDirectory();

輸出路徑:

/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF

2.Documents路徑:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

輸出路徑:

/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/Application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF/Documents

3.Library/Caches路徑:

NSString *libCachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;輸出路徑:

/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/Application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF/Library/Caches(Library/Preferences 由NSUserDefaults管理)

4.tmp路徑:

NSString *tmpPath = NSTemporaryDirectory();輸出路徑:

/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/Application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF/tmp/

5.bundle包路徑:

NSString *bundlePath = [NSBundle mainBundle].bundlePath;輸出路徑:

/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Bundle/Application/1E871CFC-2F8A-4032-ADB2-F1C797AF8D11/HelloWorld.app

二. NSFileManager 和 NSFileHandle

  就像類名字顯示的,Manger用于管理文件,創(chuàng)建文件,刪除文件,Handle用于處理文件,改變文件的內(nèi)容。

1.NSFileManager--文件創(chuàng)建

    //文件路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *fileName = @"hello.txt";    tmpPath = [tmpPath stringByAppendingPathComponent:fileName];   //這個(gè)方法會(huì)監(jiān)測(cè)是否用加'/',用來(lái)合成path        //創(chuàng)建文件    NSFileManager *fileManager = [NSFileManager defaultManager];    if(![fileManager fileExistsAtPath:tmpPath]){     //判斷下文件是否已經(jīng)存在       BOOL flag = [fileManager createFileAtPath:tmpPath contents:nil attributes:nil];        if(flag){            NSLog(@"文件創(chuàng)建成功");        }else{            NSLog(@"文件創(chuàng)建失敗");        }    }

2.NSFileManager--創(chuàng)建目錄

    //目錄路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *directorName = @"hello";    NSString *path = [tmpPath stringByAppendingPathComponent:directorName];        //創(chuàng)建目錄    NSFileManager *fileManager = [NSFileManager defaultManager];    BOOL flag = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];    if(flag){        NSLog(@"創(chuàng)建目錄成功");    }else{        NSLog(@"創(chuàng)建目錄失敗");    }

3.NSFileManager--刪除文件和目錄

    NSFileManager *fileManager = [NSFileManager defaultManager];    //目錄路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *directorName = @"test1";    NSString *path = [tmpPath stringByAppendingPathComponent:directorName];        //刪除文件    NSArray *array = [fileManager contentsOfDirectoryAtPath:path error:nil];    for(NSString * str in array){   //循環(huán)刪除test2下所有文件及目錄        NSString *filePath = [path stringByAppendingPathComponent:str];        BOOL flag = [fileManager removeItemAtPath:filePath error:nil];        if(flag){            NSLog(@"刪除成功");        }else{            NSLog(@"刪除失敗");        }    }

4.NSFileManager--其它方法

//將一個(gè)文件復(fù)制到另一個(gè)文件 [fileManager copyItemAtPath:path1 toPath:path2 error:nil];
//將一個(gè)文件移動(dòng)到另一個(gè)文件 [fileManager moveItemAtPath:path1 toPath:path2 error:nil];
//獲取文件里面的內(nèi)容 NSData * readData=[fileManager contentsAtPath:path]

5.NSFileHandle--文件寫(xiě)入

    //文件路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *fileName = @"hello.txt";    NSString *path = [tmpPath stringByAppendingPathComponent:fileName];        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];  //根據(jù)路徑初始化handle        NSString *str = @"hello";   //要寫(xiě)入的內(nèi)容    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];        [fileHandle writeData:data];   //寫(xiě)數(shù)據(jù)    [fileHandle closeFile];    //用完之后一定要關(guān)閉文件    在已有內(nèi)容,移動(dòng)光標(biāo)位置輸入:
    //文件路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *fileName = @"hello.txt";    NSString *path = [tmpPath stringByAppendingPathComponent:fileName];        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];  //根據(jù)路徑初始化handle        NSString *str = @" world";   //要寫(xiě)入的內(nèi)容    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];        [fileHandle seekToEndOfFile];  //將光標(biāo)移到文件結(jié)尾    [fileHandle writeData:data];   //寫(xiě)數(shù)據(jù)    [fileHandle closeFile];    //用完之后一定要關(guān)閉文件

6.NSFileHandle--文件讀入

    //文件路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *fileName = @"hello.txt";    NSString *path = [tmpPath stringByAppendingPathComponent:fileName];        NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];  //根據(jù)路徑初始化handle        NSData *data1 = [fileHandle readDataToEndOfFile];   //讀取到文件的末尾        [fileHandle availableData];   //查詢?cè)撐募捎脭?shù)據(jù)的個(gè)數(shù)        NSData * data3 = [fileHandle readDataOfLength:3];  //讀取指定長(zhǎng)度的文件        [fileHandle closeFile];

三. iOS數(shù)據(jù)存儲(chǔ)常用方式

1.plist文件讀寫(xiě)

  用于存儲(chǔ)常用OC數(shù)據(jù)類型,要實(shí)現(xiàn)writeToFile方法的類型。

寫(xiě)入數(shù)據(jù)-NSArray:

    //文件路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *fileName = @"str1.plist";    NSString *path = [tmpPath stringByAppendingPathComponent:fileName];        //寫(xiě)入數(shù)據(jù)    NSArray *array = @[@"a",@"b",@"c"];    [array writeToFile:path atomically:YES];   //如果沒(méi)有這個(gè)文件會(huì)創(chuàng)建文件,如果已經(jīng)存在,會(huì)覆蓋文件查看下結(jié)果:

寫(xiě)入數(shù)據(jù)-NSDictionary:

    //文件路徑    NSString *tmpPath = NSTemporaryDirectory();    NSString *fileName = @"str.plist";    NSString *path = [tmpPath stringByAppendingPathComponent:fileName];        //寫(xiě)入數(shù)據(jù)    NSDictionary *dic = @{@"aa":@"1",@"bb":@"2",@"cc":@"3"};    [dic writeToFile:path atomically:YES];查看下結(jié)果:

 讀plist數(shù)據(jù),例: 

NSArray *array = [NSArray arrayWithContentsOfFile:filePath];

2.Preferences偏好數(shù)據(jù)

    每個(gè)應(yīng)用都有一個(gè) NSUserDefault 實(shí)例,用來(lái)存取偏好設(shè)置,例如:用戶名,字體,登錄方式狀態(tài)等。不需要關(guān)心文件名稱,鍵值對(duì)存取。實(shí)際存儲(chǔ)形式為plist,所以依然只能存plist支持的類型,NSData, NSString,  NSNumber, NSDate, NSArray, NSDictionary。不支持的類型,如圖片,可以先轉(zhuǎn)成NSData,再存。

   NSUserDefault也為我們提供了一些定制好的方法,如下:

    - setBool:forKey:    - setFloat:forKey:    - setInteger:forKey:    - setDouble:forKey:    - setURL:forKey:

存入:

    //獲取設(shè)置單例    NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];    // 1、寫(xiě)入---系統(tǒng)會(huì)去存儲(chǔ)數(shù)據(jù)    [UserDefaults setBool:NO forKey:@"isLogined"];    // 2、強(qiáng)制寫(xiě)入---當(dāng)需要立即存儲(chǔ)的時(shí)候    [UserDefaults synchronize];獲取:

    //獲取設(shè)置單例    NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];        BOOL isLogined = [UserDefaults objectForKey:@"isLogined"];注:有關(guān)NSUserDefaults域的知識(shí),總結(jié)好會(huì)加上來(lái)。

3.NSKeyedArchiver歸檔

    相對(duì)于plist,NSKyeyedArchiver可以存儲(chǔ)自定義模型。相對(duì)于SQLite,NSKyeyedArchiver不適合存儲(chǔ)大批量數(shù)據(jù),在讀取某一數(shù)據(jù)時(shí),要把文件中所有數(shù)據(jù)都讀取,再尋找,如果數(shù)據(jù)量過(guò)大,性能堪憂。

    注:要?dú)w檔的模型,必須遵循NSCoding協(xié)議,然后再模型中必須實(shí)現(xiàn)兩個(gè)方法,

   歸檔:encodeWithCoder

   解檔:initWithCoder

   如果父類也遵循NSCoding,那么注意這兩句的添加,[super encodeWithCoder:encode];  self = [super initWithCoder:decoder];使用例子:

4.SQLite3

SQLite是一款輕型的嵌入式數(shù)據(jù)庫(kù),占用的資源低,速度快。

傳送門(mén):(后續(xù))

5.Core Data

傳送門(mén): (后續(xù))


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 关岭| 聂拉木县| 栾川县| 正安县| 扬州市| 孙吴县| 黔南| 中方县| 蓬安县| 龙岩市| 佛山市| 贺兰县| 夏津县| 元氏县| 寿宁县| 江达县| 伽师县| 监利县| 宣恩县| 墨竹工卡县| 玉屏| 嘉义市| 和顺县| 泰兴市| 兴隆县| 繁峙县| 静安区| 保靖县| 镇原县| 义乌市| 五原县| 莎车县| 西昌市| 迁安市| 财经| 美姑县| 岳阳县| 泉州市| 沐川县| 霍邱县| 平昌县|