前言
-
直接將數據寫在代碼里面,不是一種合理的做法。如果數據經常改,就要經常翻開對應的代碼進行修改,造成代碼擴展性低。因此,可以考慮將經常變的數據放在文件中進行存儲,程序啟動后從文件中讀取最新的數據。如果要變動數據,直接修改數據文件即可,不用修改代碼。一般可以使用屬性列表文件存儲 NSArray 或者 NSDictionary 之類的數據,這種 “屬性列表文件” 的擴展名是 plist,因此也稱為 “plist 文件”。 plist 是以 xml 文件形式存儲的。
-
如果對象是 NSString、NSArray、NSDictionary、NSData 和 NSNumber 類型,可以用這些類中實現的 writeToFile: atomically: 方法將數據寫到文件中。
-
當根據字典創建屬性列表時,字典中的鍵必須都是 NSString 對象。數組中的元素或字典中的值可以是 NSString、NSArray、NSDictionary、NSData、NSDate 和 NSNumber 對象。
-
iOS 實現的序列化方式的兩種:NSKeyedArchiver,NSPropertyListSerialization。在這兩種序列化方式中,NSData 都是序列化的目標。兩種方式的不同點在於 NSPropertyListSerialization 是針對數組和字典類型的,而 NSKeyedArchiver 是針對對象的。
1、Write 寫入方式
-
永久保存在磁盤中。具體方法為:
-
第一步:獲得文件即將保存的路徑:
-
使用 C 函數 NSSearchPathForDirectoriesInDomains 來獲得沙盒中目錄的全路徑。該函數有三個參數,目錄類型、domain mask、布爾值。其中布爾值表示是否需要通過 ~ 擴展路徑。而且第一個參數是不變的,即為 NSSearchPathDirectory 。在 iOS 中后兩個參數也是不變的,即為:NSUserDomainMask 和 YES。
NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
-
還有一種方法是使用 NSHomeDirectory 函數獲得 sandbox 的路徑,但是不能在 sandbox 的本文件層上寫文件也不能創建目錄,而應該是此基礎上創建一個新的可寫的目錄,例如 Documents, Library 或者 temp 。具體的用法為:
// 將 Documents 添加到 sandbox 路徑上 NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- 這兩者的區別就是:使用 NSSearchPathForDirectoriesInDomains 比在 NSHomeDirectory 后面添加 Documents 更加安全。因為該文件目錄可能在未來發送的系統上發生改變。
-
-
第二步:生成在該路徑下的文件:
// fileName 就是保存文件的文件名 NSString *FileName = [documentPath stringByAppendingPathComponent:@"fileName"];
-
第三步:往文件中寫入數據:
// 將 NSData 類型對象 data 寫入文件,文件名為 FileName [data writeToFile:FileName atomically:YES];
-
第四步:從文件中讀出數據:
// 從 FileName 中讀取出數據 NSData *data = [NSData dataWithContentsOfFile:FileName options:0 error:NULL];
-
2、Plist 文件的讀寫
NSString *arrayPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/arrayToPList.plist"];
NSString *dictionaryPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/dictionaryToPList.plist"];
// 待寫入數據
NSArray *array = @[@"bei", @"jing", @"huan", @"ying", @"nin"];
// 待寫入數據
NSDictionary *dictionary = @{@"name":@"chen chao", @"age":@"18", @"info":@"Good Teacher"};
// 寫 Plist 文件
// 數組寫入 plist 文件
BOOL bl1 = [array writeToFile:arrayPath atomically:YES];
// 字典寫入 plist 文件
BOOL bl2 = [dictionary writeToFile:dictionaryPath atomically:YES];
// 讀 Plist 文件
NSArray *arrayFromPlist = [NSArray arrayWithContentsOfFile:arrayPath];
NSDictionary *dicFromPList = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];
3、Plist 序列化
NSString *arrayPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/arrayPropertyList.plist"];
NSString *dictionaryPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/dictionaryPropertyList.plist"];
// 待寫入數據
NSArray *array = @[@"bei", @"jing", @"huan", @"ying", @"nin"];
// 待寫入數據
NSDictionary *dictionary = @{@"name":@"chen chao", @"age":@"18", @"info":@"Good Teacher"};
// 序列化,將數據轉換成 XML 格式的文件
NSData *arrayData = [NSPropertyListSerialization dataWithPropertyList:array
format:NSPropertyListXMLFormat_v1_0
options:1
error:nil];
NSData *dictionaryData = [NSPropertyListSerialization dataWithPropertyList:dictionary
format:NSPropertyListXMLFormat_v1_0
options:1
error:nil];
// 輸出到 .txt 格式文件中
BOOL bl1 = [arrayData writeToFile:arrayPath atomically:YES];
BOOL bl2 = [dictionaryData writeToFile:dictionaryPath atomically:YES];
// 反序列化
NSArray *arrayFromeFile = [NSArray arrayWithContentsOfFile:arrayPath];
NSDictionary *dicitionaryFromeFile = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];
4、Plist 文件的使用
4.1 Plist 文件的創建
4.2 Plist 文件的解析
// 獲得 Plist 文件的全路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
// 加載 plist 文件
NSArray *shops = [NSArray arrayWithContentsOfFile:path];
4.3 Plist 文件的解析過程
4.4 Plist 的使用注意
-
plist 的文件名不能叫做 “info”、“Info” 之類的。
-
添加 plist 等文件資源的時候,一定要勾選下面的選項。