查询地址: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. La=LaULb (单链表)

    #include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...

  2. Spring 之 控制反转(IoC), 依赖注入(DI)和面向切面(AOP)

    关于依赖注入, 这篇博文写的非常简单易懂. https://github.com/android-cn/blog/tree/master/java/dependency-injection 此外, 博 ...

  3. Linux 系统之Sysvinit

    子贡问为仁.子曰:“工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者.”——孔子(春秋)<论语·卫灵公> [工欲善其事,必先利其器] 掌握一门技术,知道其发展历程是非常重要的 ...

  4. JVM典型配置

    堆大小设置: JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存 限制.32位系统下,一般限制在1.5G~2G:64为 ...

  5. protel99se中做拼板图解

    很多时候我们要在protel99se中做拼板, 但是通常在复制进行拼版的时候会出现如下的效果,元件被重新命名了. 而无法达到我们需要的像下图的效果 那我们怎么办,才能达到上图的效果呢?其实操作很简单. ...

  6. bulk insert data into database with table type .net

    1. Create Table type in Sqlserver2008. CREATE TYPE dbo.WordTable as table ( [WordText] [nchar]() NUL ...

  7. JqueryMobile新手问题大全

    Jquery mobile 新手问题总汇 34 2013-04-22 / 分类:JqueryMobile / 标签:JqueryMobile,Jqm 此文章将会持续更新,主要收录一些新手比较常见的问题 ...

  8. iOS开发之第三方登录微信-- 史上最全最新第三方登录微信方式实现

    项目地址 :    https://github.com/zhonggaorong/weixinLoginDemo 最新版本的微信登录实现步骤实现: 1.在进行微信OAuth2.0授权登录接入之前,在 ...

  9. 设计模式值六大原则——开闭原则(OCP)

    开闭原则(Open Closed Principle)是Java世界里最基础的设计原则,它指导我们如何建立一个稳定的.灵活的系统. 定义: 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. S ...

  10. [LeetCode]题解(python):062-Unique Paths

    题目来源: https://leetcode.com/problems/unique-paths/ 题意分析: 给定两个整型m,n.判断从一个m×n的矩阵里面,从(0,0)走到(m-1,n-1)一共有 ...