生活随笔
收集整理的這篇文章主要介紹了
数据存储之属性列表Plist
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常用的數據存儲有屬性列表、偏好設置、歸檔、sqlite、coreData。上一博客了解了沙盒,現在了解下屬性列表Plist。
通常通過NSArray、NSDictionary集合類的WriteToFile:atomically方法將他們存儲到屬性列表中。在屬性列表能保存的數據類型如下
所以可以序列化的類有以下這些:
NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSData、NSMutableData、NSDate、NSString、NSMutableString、NSNumber
對Boolean類型的數據進行讀寫時,需先轉為NSNumber類型,然后通過NSNumber的boolValue方法讀取。
//
// ViewController.m
// Plist
//
// Created by City--Online on 15/4/21.
// Copyright (c) 2015年 CYW. All rights reserved.
//#import "ViewController.h"
#import "Student.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];NSArray *array= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);NSString *path=[array objectAtIndex:0];#if 0 //數組NSString *filePath=[path stringByAppendingPathComponent:@"students.plist"];NSLog(@"%@",filePath);
#if 0//數組寫數據NSArray *array1=[[NSArray alloc]initWithObjects:@"a",[NSDate date],@20.9,[NSNumber numberWithBool:YES],nil]//YES 通過atomically參數讓該方法將數據寫入輔助文件,而不是寫入指定位置。成功寫入該文件后,該輔助文件將被復制到第一個參數指定的位置.這是更安全的寫入方法,因為如果應用程序在保存期間崩潰,則現有文件不會被破壞。雖增加開銷,但在大多數情況還是值得的。[array1 writeToFile:filePath atomically:YES];
#elif 1 //數組讀數據
// NSArray *array1=[[NSArray alloc]initWithContentsOfFile:filePath];NSArray *array1=[NSArray arrayWithContentsOfFile:filePath];for (NSString *s in array1) {NSLog(@"%@",s);}
#endif#elif 1 //字典NSString *filePath=[path stringByAppendingPathComponent:@"studentsdic.plist"];NSLog(@"%@",filePath);
#if 0//字典寫入NSDictionary *dic=[[NSDictionary alloc]initWithObjects:@[@"a",@"b",@"c"] forKeys:@[@"1",@"2",@"3"]];[dic writeToFile:filePath atomically:NO];
#elif 1//字典讀數據
// NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:filePath];NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:filePath];for (NSString * s in dic.allKeys) {NSLog(@"%@",[dic objectForKey:s]);}
#endif
#endif}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
// //獲取沙盒根目錄
// NSString *home=NSHomeDirectory();
// NSLog(@"沙盒根目錄:%@\n\n",home);
//
// //獲取Documents目錄 不建議采用
// NSString *documents=[home stringByAppendingPathComponent:@"Documents"];
// NSLog(@"字符串拼接獲取Documents:%@\n\n",documents);
//
// //NSUserDomainMask 代表從用戶文件夾下找
// //YES 代表展開路徑中的波浪字符“~” NO ~/Documents
// NSArray *array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);
// // 在iOS中,只有一個目錄跟傳入的參數匹配,所以這個集合里面只有一個元素
// NSString *documents1=[array objectAtIndex:0];
// NSLog(@"通過方法NSSearchPathForDirectoriesInDomains獲取Documents:%@\n\n",documents1);
//
// //獲取tmp文件目錄
// NSLog(@"tmp 文件目錄:%@\n\n",NSTemporaryDirectory());
//
// //獲取Library/Caches:
// NSArray *arrayCaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
// NSLog(@"Library/Caches:%@",arrayCaches[0]);
//
// //Library/Preference:通過NSUserDefaults類存取該目錄下的設置信息
?
總結
以上是生活随笔為你收集整理的数据存储之属性列表Plist的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。