XML屬性列表(plist)歸檔



XML屬性列表(plist)歸檔


1> 理解:

a. 屬性列表是一種XML格式的文件,拓展名爲plist;

b.如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表文件中;

c. 自定義對象不能做 plist 存儲,因爲它沒有writeToFile:atomically:這個方法;

d. 第三個參數 一定是yes,如果爲no 系統會把你的應用的沙盒路徑替換成 ~;

e. 字符串: 雖然能夠做plist存儲,但是plist本身不支持字符串,其次如果字符串是中文,那麼存儲的plist文件是打不開的。


2>  獲取doc目錄

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSString * filePath = [docPath stringByAppendingPathComponent:@"xx.plist"];

// NSDocumentDirectory : 搜索哪個文件夾

// NSUserDomainMask : 在哪搜索

// atomically:線程安全的


3> 字典的存取

// 存字典

NSDictionary* dict = @{ @"key" :@"value", @"key1" : @"value1" };

[dict writeToFile:filePath atomically:YES];

// 取字典

NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

NSLog(@"%@", dict);

4> 數組的存取

// 存數組

NSArray* array = @[ @"傳智播客",@"你好" ];

[array writeToFile:filePath atomically:YES];

// 取數組

NSArray* array = [NSArray arrayWithContentsOfFile:filePath];

NSLog(@"%@", array); 


XML屬性列表(plist)歸檔


1> 理解:

a. 屬性列表是一種XML格式的文件,拓展名爲plist;

b.如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表文件中;

c. 自定義對象不能做 plist 存儲,因爲它沒有writeToFile:atomically:這個方法;

d. 第三個參數 一定是yes,如果爲no 系統會把你的應用的沙盒路徑替換成 ~;

e. 字符串: 雖然能夠做plist存儲,但是plist本身不支持字符串,其次如果字符串是中文,那麼存儲的plist文件是打不開的。


2>  獲取doc目錄

NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSString * filePath = [docPath stringByAppendingPathComponent:@"xx.plist"];

// NSDocumentDirectory : 搜索哪個文件夾

// NSUserDomainMask : 在哪搜索

// atomically:線程安全的


3> 字典的存取

// 存字典

NSDictionary* dict = @{ @"key" :@"value", @"key1" : @"value1" };

[dict writeToFile:filePath atomically:YES];

// 取字典

NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

NSLog(@"%@", dict);

4> 數組的存取

// 存數組

NSArray* array = @[ @"傳智播客",@"你好" ];

[array writeToFile:filePath atomically:YES];

// 取數組

NSArray* array = [NSArray arrayWithContentsOfFile:filePath];

NSLog(@"%@", array); 


Gods determine what you're going to be.

人生的奮鬥目標決定你將成爲怎樣的人。


發佈了53 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章