数据存储之属性列表Plist
2023-09-14 08:57:58 时间
常用的数据存储有属性列表、偏好设置、归档、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 () @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
// //获取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类存取该目录下的设置信息
大量文件名记录的树形结构存储 十多年来,NAS中已经存在的目录和文件达到10亿之多,在设计和开发备份系统的过程中碰到了很多挑战,本文将分享大量文件名记录的树形结构存储实践。
社会主义 从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.
相关文章
- C# Message类的属性Msg所关联的消息ID
- iOS开发 -- 为本地文件添加自定义属性的工具类
- 让IE7/8使用CSS中first-child和last-child样式属性
- 第六十八节,htnl全局属性和其他属性
- python3元组,列表的几个属性
- 将某一项的属性显示成列表
- 控制输入文本框的宽度属性size
- Scala抽象属性的继承
- Component Configuration.js - 所有支持属性列表 - configuration priority
- CSS属性选择器技巧
- 【Android Gradle 插件】DexOptions 配置 ⑧ ( preDexLibraries 属性配置 | threadCount 属性配置 )