Read and Write NSArray, NSDictionary and NSSet to a File
2015-06-03 08:45
411 查看
查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset-to-a-file.html
With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading both NSArray and NSDictionary objects, the same logic would apply to an NSSet (or other collection type).
The example below starts by populating both an array and dictionary, each using the Objective-C literal syntax. Read more about using NSArray literals andNSDictionary literals.
– Create the NSArray and NSDictionary objects
– Obtain the path to write/read the files (application Documents directory)
– Append filenames to path for each collection type
– Use the ‘writeToFile:’ method of each object to save contents to file
The output in the console window is below:
With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading both NSArray and NSDictionary objects, the same logic would apply to an NSSet (or other collection type).
The example below starts by populating both an array and dictionary, each using the Objective-C literal syntax. Read more about using NSArray literals andNSDictionary literals.
Read and Write Collections to File
The process is straightforward:– Create the NSArray and NSDictionary objects
– Obtain the path to write/read the files (application Documents directory)
– Append filenames to path for each collection type
– Use the ‘writeToFile:’ method of each object to save contents to file
NSString *arrayPath; NSString *dictPath; NSArray *array = @[@"IPA", @"Pilsner", @"Stout"]; NSDictionary *dictionary = @{@"key1" : array, @"key2" : @"Hops", @"key3" : @"Malt", @"key4" : @"Yeast" }; // Get path to documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); if ([paths count] > 0) { // Path to save array data arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"]; // Path to save dictionary dictPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"dict.out"]; // Write array [array writeToFile:arrayPath atomically:YES]; // Write dictionary [dictionary writeToFile:dictPath atomically:YES]; } |
Read NSArray NSDictionary NSSet From File
The code to read back from the collections is equally as simple, using the methods arrayWithContentsOfFile: and dictionaryWithContentsOfFile: read from the file paths created previously to read the save data:// Read both back into new NSArray and NSDictionary object NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath]; NSDictionary *dictFromFile = [NSDictionary dictionaryWithContentsOfFile:dictPath]; // Print the contents for (NSString *element in arrayFromFile) NSLog(@"Beer: %@", element); for (NSString *key in dictFromFile) NSLog(@"%@ : %@", key, [dictionary valueForKey:key]); |
Beer: IPA Beer: Pilsner Beer: Stout key1 : ( IPA, Pilsner, Stout ) key2 : Hops key3 : Malt key4 : Yeast |
相关文章推荐
- [Android实例] 细谈Ble4.0 APP开发
- 第十三周项目三:立体类族共有的抽象类
- elasticsearch的实现全文检索
- MySQL按照汉字拼音首字母排序
- OJ对象数组输入与输出
- 第十三周 项目2-形状类族的中的纯虚函数
- 第13周 【项目 - 链表类】阅读项目
- 第十三周项目1--动物这样叫(2)
- C++刷题——2802: 判断字符串是否为回文
- 第13周项目 程序阅读(4)
- 领域驱动设计系列(3)有选择性的使用领域驱动设计
- 记一次云计算测试实验-openstack-icehouse-环境介绍
- 十三周项目一
- C语言之第六章 常量 宏 函数
- 百度平台首个神秘创业项目悄然上线(
- 连接3600行,百度靠什么来实现?
- 领域驱动设计系列(2)浅析VO、DTO、DO、PO的概念、区别和用处
- 领域驱动设计系列(1)通过现实例子显示领域驱动设计的威力
- 树链剖分学习
- java Class.forName("") .newInstance();