生活随笔
收集整理的這篇文章主要介紹了
数据存储之属性列表Plist
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
常用的數(shù)據(jù)存儲(chǔ)有屬性列表、偏好設(shè)置、歸檔、sqlite、coreData。上一博客了解了沙盒,現(xiàn)在了解下屬性列表Plist。
通常通過NSArray、NSDictionary集合類的WriteToFile:atomically方法將他們存儲(chǔ)到屬性列表中。在屬性列表能保存的數(shù)據(jù)類型如下
所以可以序列化的類有以下這些:
NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSData、NSMutableData、NSDate、NSString、NSMutableString、NSNumber
對(duì)Boolean類型的數(shù)據(jù)進(jìn)行讀寫時(shí),需先轉(zhuǎn)為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 //數(shù)組NSString *filePath=[path stringByAppendingPathComponent:@"students.plist"];NSLog(@"%@",filePath);
#if 0//數(shù)組寫數(shù)據(jù)NSArray *array1=[[NSArray alloc]initWithObjects:@"a",[NSDate date],@20.9,[NSNumber numberWithBool:YES],nil]//YES 通過atomically參數(shù)讓該方法將數(shù)據(jù)寫入輔助文件,而不是寫入指定位置。成功寫入該文件后,該輔助文件將被復(fù)制到第一個(gè)參數(shù)指定的位置.這是更安全的寫入方法,因?yàn)槿绻麘?yīng)用程序在保存期間崩潰,則現(xiàn)有文件不會(huì)被破壞。雖增加開銷,但在大多數(shù)情況還是值得的。[array1 writeToFile:filePath atomically:YES];
#elif 1 //數(shù)組讀數(shù)據(jù)
// 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//字典讀數(shù)據(jù)
// 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中,只有一個(gè)目錄跟傳入的參數(shù)匹配,所以這個(gè)集合里面只有一個(gè)元素
// 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類存取該目錄下的設(shè)置信息
?
總結(jié)
以上是生活随笔為你收集整理的数据存储之属性列表Plist的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。