dhilipsiva
dhilipsiva

Reputation: 3718

.plist file writing failed! what is wrong with this code?

Nothing is written in my plist file after this code. What is wrong with my code?

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"aFile.plist"];
NSMutableDictionary *reqData = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
/*
some modifications to "reqData"
*/
[reqData writeToFile:finalPath atomically:YES];

Nothing is written in file. what could be the problem?

Upvotes: 0

Views: 1026

Answers (4)

Dr.Kameleon
Dr.Kameleon

Reputation: 22810

You are trying to write the file back to the app bundle.

That's what's most likely causing the error.

What if you try writing it somewhere else (e.g. in your desktop folder)?

Upvotes: 1

cnu
cnu

Reputation: 815

First Thing to remember:

You can read a plist file from resources but you can't modify it.

if you want to modify,

  1. copy that file to Documents directory
  2. copy contents of plist into array or dictionary depending on its type
  3. make changes you want
  4. Finally.... save it back to documents directory

that do the trick

Upvotes: 0

Thomas Hajcak
Thomas Hajcak

Reputation: 476

If you're including the plist with your application, you'll want to copy that file into the Documents directly when the app first starts up (if it hasn't already been copied there). Then, any read and write operations you want to do on the plist should be done from the copy in the Documents directory instead of the version in the app bundle.

Upvotes: 0

Hailei
Hailei

Reputation: 42163

You'd better write to Document folder:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);
NSString *path = [paths objectAtIndex:0];

Update

According to NSDictionary Class Reference:

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

Is there any objects with a type other than these valid ones in your whole dictionary?

Upvotes: 2

Related Questions