查询地址: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.

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]);

The output in the console window is below:

Beer: IPA
Beer: Pilsner
Beer: Stout
key1 : (
IPA,
Pilsner,
Stout
)
key2 : Hops
key3 : Malt
key4 : Yeast
 
 
 
 
 
 

最新文章

  1. scenejs的一点Cameras小笔记
  2. j嵌入式f_os之定时管理
  3. Codeforces 624
  4. Ajax跨域请求
  5. 回到过去美好的时光——源代码版本管理Always Use source code Control
  6. selenium结合sikuliX操作Flash网页
  7. R实战之热点图(HeatMap)
  8. C++模板常用使用方法介绍
  9. Rails--content_for和yield
  10. 如何用JS获取ASP.net中的textbox的值 js获不到text值
  11. [POJ] 2239 Selecting Courses(二分图最大匹配)
  12. Makefile学习(三)执行make
  13. Struts ActionForm简单理解
  14. Activiti 实战篇 小试牛刀
  15. Java8新特性之四:接口默认方法和静态方法
  16. Linux-Nginx+rtmp+ffmpeg搭建流媒体服务器
  17. MyBatis动态代理执行原理
  18. docker-compose hello word
  19. 关于STM32CubeMX使用LL库设置PWM输出
  20. springboot配置双数据源 MySQL和SqlServer

热门文章

  1. PGA_AGGREGATE_TARGET 原理
  2. 一种CentOS图形界面的修复方法
  3. android-意图Intent
  4. SIGAR - System Information Gatherer And Reporter
  5. 通往WinDbg的捷径
  6. ThinkPHP 3.1.2 模板中的基本语法<1>
  7. Windows Azure 即将更名
  8. In Action(最短路+01背包)
  9. Jedis中的一致性hash
  10. ActionScript3游戏中的图像编程(连载十七)