开发者

write a dictionary to plist on iphone

开发者 https://www.devze.com 2023-04-09 02:53 出处:网络
I want to add a new dictionary to the existing plist file, how can I do that, I can read from the file, but with the same approach, writing doesn\'t work. My plist structure looks like this:

I want to add a new dictionary to the existing plist file, how can I do that, I can read from the file, but with the same approach, writing doesn't work. My plist structure looks like this:

Root         Array
  |__item0   Dictionary
  |__item1   Dictionary
  |__item2   Dictionary

The code of writing to the file looks like this:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentDirectory stringByAppendingPathComponent:@"list.plist"];

if (![fileManager fileExistsAtPath:plistPath]) {
    plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
}

NSMutableArray *dataRoot = [NSMutableArray arrayWithContentsOfFile:plistPath];

NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:@"value1" forKey:@"1"];
[item setObject:@"value2" forKey:@"2"];
[item setObject:@"value3" forKey:@"3"];
[dataRoot addObject:item];
[dataRoot writeToFile:plistPath atomically:YES];
[item release];

Can anyone help me with this, thanks!

I have updated my code, but it still doesn't work...And the problem is list.plist fi开发者_JAVA百科le doesn't get copied to ~/Documents directory.


You are trying to write the main bundle which is no-no on iOS. Please read the A Few Important Application Directories.

But your coding boils down to the following:

  1. Check if plist is not in Application_Home/Documents/
  2. Copied original plist from main bundle is the answer to step 1 is false.
  3. Open the plist in the Documents directory
  4. Update NSMutableArray
  5. Write to file the plist


I think code is not wrong.

Have you check plistPath, dataRoot, and item?

They have correct value?


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];


if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {


bingoArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

}

[bingoArray writeToFile:filePath atomically:YES];


You can only save primitive types by default.

try out this link.

http://deadpanic.com/howtosave

He explains how to save any object to disk by using NSCoding protocol.

If you are working only with primitives then I apologize for wasting your time. But this is the path I took to write my custom stuff to disk. And I thought it was just an array issue.

Edit:

I just noticed... It looks like you are trying to write to an array in the Main Bundle. These files are read only.

You need to save a copy of the file in the Documents directory.

try this out to find the documents directory

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// Change your plistPath to be this
plistPath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];
[dataRoot writeToFile:plistPath atomically:YES];

put your plist in this folder and you can write to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号