简体   繁体   中英

how to create plist file in xcode 4.3.2

I am trying to create my own plist file to be used in my application, I am using apple documents as refrence for doing this.

However, I have read that if you create your own plist you need to place it in the resource folder in your applications build. The issue with that is that I have no resources folder in my build... so I am woundering what should I do?.. I have read this guys answere here , he says its fine to just place the plist file in the supporting Files folder.. is this okay to do in regards to allowing the plist to be read and written too?

To read and write to plist the best practice is to copy it to document root if you want to access it through bundle you don't have write permission. I have provided a snap shot of the code here and how you can accomplish this.

NSError *err;
NSFileManager *fileManager = [NSFileManager defaultManager];
//getting the path to document directory for the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [paths objectAtIndex:0];
NSString *path = [documentDir stringByAppendingPathComponent:@"YourFile.plist"];

//checking to see of the file already exist
if(![fileManager fileExistsAtPath:path])
{
    //if doesnt exist get the the file path from bindle
    NSString *correctPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YourFile.plist"];
    //copy the file from bundle to document dir
    [fileManager copyItemAtPath:correctPath toPath:path error:&err];      
}

You can put that plist file anywhere you want. The important thing will be copying it into the bundle. So to be sure for that check project settings>build phases>copy bundle resources

You can open project settings by left-clicking on your project in the project navigator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM