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

Read and Write NSArray, NSDictionary and NSSet to a File的更多相关文章

  1. 一些NSArray,NSDictionary,NSSet相关的算法知识

    iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...

  2. [转]一些NSArray,NSDictionary,NSSet相关的算法知识

    iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...

  3. Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型

    Foundation框架之NSArray.NSDictionary.NSSet及其Mutable类型 目录 概述——对Foundation框架集合类的理解 NSArray NSDictionary N ...

  4. Objective-C学习篇08—NSDictionary与NSSet

    NSDictionary与NSMutableDictionary NSSet与NSMutableSte 字典 字典:字典分为可变字典NSDictionary和不可变字典NSMutableDiction ...

  5. NSDictionary NSMutableDictionary NSSet NSMutableSet

    //description只是返回了一个字符串 //    [person description]; //        //如果想要打印需要NSLog //    NSLog(@"%@& ...

  6. NSData NSDate NSString NSArray NSDictionary 相互转换

    // NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSDa ...

  7. Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法

    Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...

  8. NSData NSDate NSString NSArray NSDictionary 相互转化

    //    NSData  NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; ...

  9. Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区

    Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...

随机推荐

  1. 关于Staruml与powerdesigner启动使用中的问题

    问题描述:启动StarUML时,报System Error.Code:1722.RPC服务器不可用的错误! 如下: 这时候: 只需要开启Print Spooler服务即可!在“控制面板中-->管 ...

  2. input file 模拟

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  3. 13-C语言字符串函数库

    目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...

  4. pcap文件格式解析

    pcap文件格式是常用的数据报存储格式,包括wireshark在内的主流抓包软件都可以生成这种格式的数据包 下面对这种格式的文件简单分析一下:    pcap文件的格式为:  文件头    24字节  ...

  5. JVM报错提示

    持久代被占满 异常:java.lang.OutOfMemoryError: PermGen space 说明: Perm空间被占满.无法为新的class分配存储空间而引发的异常.这个异常以前是没有的, ...

  6. rhApp遇到的项目问题

    1.如果有多人同时操作一个桌台的情况下,如何处理: 2.index页面点击清空的时候是否要把桌台一起清掉: 3.账单界面已结账的小单背景色是否需要和未结账的不同:

  7. golang ODBC 访问access数据库(问题解决之心理路程)

    最近项目需要,需要操作access,以前是用VC++ OLE访问,网络用ACE库,感觉很庞大...决定用go试试 网上用的最多的就是这个https://github.com/weigj/go-odbc ...

  8. Microsoft 收购 Apiphany

    StevenMartinMS 2013 年 10 月 23 日上午 10:00 今天,我高兴地宣布我们收购了业界领先的 API 管理交付平台 - Apiphany. 应用程序可扩展性已经不算什么新鲜事 ...

  9. pyrailgun 0.24 : Python Package Index

    pyrailgun 0.24 : Python Package Index pyrailgun 0.24 Download pyrailgun-0.24.zip Fast Crawler For Py ...

  10. 1148 - Mad Counting(数学)

    1148 - Mad Counting   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...