如何在xcode 4.3.2中创建plist文件

时间:2012-04-02 22:43:21

标签: iphone ios plist

我正在尝试创建自己的plist文件以便在我的应用程序中使用,我使用apple documents作为参考。

但是,我已经读过,如果你创建自己的plist,你需要将它放在应用程序构建的资源文件夹中。这个问题是我的构建中没有资源文件夹...所以我想知道我应该怎么做?..我已经读过这些人回答here,他说只需放置plist文件就好了在支持Files文件夹..对于允许读取和写入plist这样做是否可行?

2 个答案:

答案 0 :(得分:5)

要读取和写入plist,最佳做法是将其复制到文档根目录,如果要通过捆绑包访问它而没有写入权限。我在这里提供了代码的快照,以及如何实现这一目标。

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];      
}

答案 1 :(得分:3)

您可以将该plist文件放在任何您想要的位置。 重要的是将它复制到捆绑包中。所以要确保检查 project settings>build phases>copy bundle resources

您可以通过在项目导航器中左键单击项目来打开项目设置。

相关问题