博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Read and Write NSArray, NSDictionary and NSSet to a File
阅读量:5153 次
发布时间:2019-06-13

本文共 2353 字,大约阅读时间需要 7 分钟。

查询地址:

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  and.

 

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 directoryNSArray *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 objectNSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];NSDictionary *dictFromFile = [NSDictionary dictionaryWithContentsOfFile:dictPath]; // Print the contentsfor (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: IPABeer: PilsnerBeer: Stoutkey1 : (    IPA,    Pilsner,    Stout)key2 : Hopskey3 : Maltkey4 : Yeast

转载于:https://www.cnblogs.com/wvqusrtg/p/4548239.html

你可能感兴趣的文章
Xamarin Visual Studio不识别JDK路径
查看>>
菜鸟“抄程序”之道
查看>>
Ubuntu下关闭防火墙
查看>>
TCP/IP 邮件的原理
查看>>
w3m常用快捷键
查看>>
【Unity 3D】学习笔记四十一:关节
查看>>
原型设计工具
查看>>
windows下的C++ socket服务器(4)
查看>>
css3 2d转换3d转换以及动画的知识点汇总
查看>>
【Java】使用Eclipse进行远程调试,Linux下开启远程调试
查看>>
js对象属性方法
查看>>
对Vue为什么不支持IE8的解释之一
查看>>
PHP队列
查看>>
程序代码记Log
查看>>
Maven安装配置
查看>>
ORA-10635: Invalid segment or tablespace type
查看>>
计算机改名导致数据库链接的诡异问题
查看>>
Windows 8 操作系统 购买过程
查看>>
软件工程课程-个人编程作业
查看>>
Java8内存模型—永久代(PermGen)和元空间(Metaspace)(转)
查看>>