Read and Write NSArray, NSDictionary and NSSet to a File
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; |
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 |
The output in the console window is below:
Beer: IPA |
Read and Write NSArray, NSDictionary and NSSet to a File的更多相关文章
- 一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- [转]一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型
Foundation框架之NSArray.NSDictionary.NSSet及其Mutable类型 目录 概述——对Foundation框架集合类的理解 NSArray NSDictionary N ...
- Objective-C学习篇08—NSDictionary与NSSet
NSDictionary与NSMutableDictionary NSSet与NSMutableSte 字典 字典:字典分为可变字典NSDictionary和不可变字典NSMutableDiction ...
- NSDictionary NSMutableDictionary NSSet NSMutableSet
//description只是返回了一个字符串 // [person description]; // //如果想要打印需要NSLog // NSLog(@"%@& ...
- NSData NSDate NSString NSArray NSDictionary 相互转换
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSDa ...
- Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法
Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...
- NSData NSDate NSString NSArray NSDictionary 相互转化
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; ...
- Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区
Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...
随机推荐
- 各种输入速度比较(cin,scanf,getchar,pascal的read)
每个程序都是纯读入与1个输出.输入数据分别有1e2,1e3,1e4,1e5,1e6,1e7个数. C++使用getchar()读入是最快的.流输入是非常非常慢! 比赛还是用gechar比较好...
- linux cpu亲和性设置
1.设置进程的cpu亲和性程序如下, #Using:g++ setCpuAffinity.c -o setCpuAffinity #include<sys/types.h> #includ ...
- MVC-05 Model(2)
五.使用Code First数据库迁移 当Entity Framework Code First的数据模型发生异动时,默认会引发一个System.InvalidOpertaionException异常 ...
- ruby2.0(rails)以后版本的debug
很喜欢RUBY(RAILS),认识也好久好久了,但是说实话,从来没用ROR写过什么东西,都是小打小闹,做些自娱自乐的东西,碰到什么问题,基本仔细看看,加上几个LOG就找到原因了,从来没想过要DEBUG ...
- 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication 基于语句和基于行复制的优点和缺点: ...
- Linux批量重命名文件
五种方法实现Linux批量重命名文件 Linux批量重命名文件是指对某些特定的文件统一进行重新命名,以改变原来一批文件的名称,这里介绍五种方法来实现. Linux批量重命名文件会涉及到改变一个字母.改 ...
- android 之Fragment(官网资料翻译)
原文地址: http://blog.csdn.net/lilu_leo/article/details/7671533 *************************** 正文分割线 ***** ...
- JSP 9 大内置对象详解
内置对象特点: 1. 由JSP规范提供,不用编写者实例化. 2. 通过Web容器实现和管理 3. 所有JSP页面均可使用 4. ...
- 【剑指offer】第一个仅仅出现一次的字符
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27106997 题目描写叙述: 在一个字符串(1<=字符串长度<=10000,所 ...
- Live Writer Test
测试下LiveWriter写CNblog: 1.Source code plug-in: @Override public List getAll(String orgid,String start, ...