使用根元素初始化 plist
我正在尝试为 iPhone 创建一个 RSS 阅读器,我要做的第一件事就是创建一个 plist 来保存一堆 NSDictionary。但是当在我的 appDelegate (didFinishLaunchingWithOptions:) 中创建 plist 时,根元素是 NSDictionary,但我想要一个 NSArray 来保存其他元素。
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}
filePath 变量是文档文件夹的路径。创建 plist 的代码运行正常。
I am trying to create an RSS reader for iPhone, first thing i trying to do is create a plist to hold a bunch of NSDictionary. But when a create the plist in my appDelegate (didFinishLaunchingWithOptions:) the root elements is a NSDictionary, but i want a NSArray to hold the other elements.
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}
the filePath variable is the path to the document folder. the code to create the plist is running ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能只创建一个空数组(NSArray)并将其写入文件吗?
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/writeToFile:atomically:
并以类似的方式执行
[NSArray arrayWithContentsOfFile:path] (或者 NSMutableArray 如果您愿意)
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes /NSArray_Class/NSArray.html#//apple_ref/occ/clm/NSArray/arrayWithContentsOfFile:
Can't you just create a empty array (NSArray) and write that to file?
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/writeToFile:atomically:
And in a similar fashion do
[NSArray arrayWithContentsOfFile:path] (or a NSMutableArray if you prefer that)
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/clm/NSArray/arrayWithContentsOfFile:
分配一个
NSMutableArray
,然后使用[NSMutableArray addObject:dictionary
添加字典。您可以选择何时将带有字典的数组写入持久存储(文件),但不需要先写入空文件。只需确保在进入后台和/或退出应用程序之前更新文件
Alloc an
NSMutableArray
, then use[NSMutableArray addObject:dictionary
to add the dictionaries.At what point in time you write the Array with the dictionaries to persistent storage (file) is your choice, but you don't need to write an empty file first. Just ensure you update the file before entering background and/or before exiting the App