views:

278

answers:

1

I am having a hard time figuring out how to get the property list file that was created using Xcode. I created a property list file using array with NSString members. I want to grab that file and get all the NSString members and save it to a UITextField. But my problem is that I can't see that file. I don't know if I'm looking in the wrong path, or I don't know where the property file is saved.

+1  A: 

Most likely, if the file was added via Xcode, the file is in your bundle. To open it use:

NSDictionary *plist = [NSDictionary arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NameOfFile" ofType:@"plist"]];

Run this in the simulator and then look in the Finder, in Library/Application Support/iPhone Simulator/Applications/... to see if you can find the file, open it in and double check it to make sure Xcode added the file.

To make a copy in your Documents folder:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"My.plist"];
if (![plist writeToFile:path atomically:YES])
    NSLog(@"not successful in writing the high scores");
mahboudz
Thanks alot. But how can I save my plist file to Documents folder?
sasayins
Once you read it in successfully, you can immediately write it back out using writeToFile, giving it the path to your Documents folder. Know how to do that?
mahboudz
I see. thanks. so i think i need get my Documents folder path then use the writeToFile method. But when I successfully created the second plist in the document, I am creating two plist with the same data? Or should I delete the plist from the app bundle?
sasayins
The plist in the bundle is read only from the iPhone so a) you can't delete it and b) you couldn't save any changes to it. Therefore yes it is standard that you'd have 2 copies - one original, untouched, and one which records changes as your user uses the app.
h4xxr
oh i see. thanks a lot. so i think it's not very efficient to have two copies of the file. Is there any way to explicitly save the plist file in the Document folder?
sasayins
The easiest way to only have one copy is if you download a copy from some server into the Documents folder. This may be more work than just living with two copies.There is an added advantage to having two copies; you can provide the user with a Revert function to undo any changes to the plist file. I am assuming that you are going to allow the user to modify the version in the Documents folder. If, however, you are not editing the plist file, and keeping it as read-only, then just use it in your bundle and don't make a copy in Documents.
mahboudz
I edited to add code to show you how to write it into your Documents folder.
mahboudz