是否可能保存NSMutableArray或NSDictionary数据作为文件在iOS中? [英] is it possible to save NSMutableArray or NSDictionary data as file in iOS?
本文介绍了是否可能保存NSMutableArray或NSDictionary数据作为文件在iOS中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 NSMutableArray
或 NSDictionary
另存为永久档案。然后,我想稍后重新打开该文件。
I want to save an NSMutableArray
or NSDictionary
as a permanent file. Then, I would like to reopen the file later.
这是可能吗?
推荐答案
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
[myArray writeToFile:filePath atomically:YES];
从文件读取
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
myArray = [NSArray arrayWithContentsOfFile:filePath];
如果文件不存在,myArray将为nil。和NSDictionary有类似的方法。请检查这些方法的详细信息的参考。
myArray will be nil if file is not present. And NSDictionary has similar methods. Please check the reference for the details of these methods.
这篇关于是否可能保存NSMutableArray或NSDictionary数据作为文件在iOS中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文