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 variab开发者_运维百科le is the path to the document folder. the code to create the plist is running ok.
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:
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
精彩评论