如何在xcode 4.3.2中创建plist文件



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

然而,我已经读到,如果您创建自己的plist,您需要将其放置在应用程序构建中的资源文件夹中。问题是我的构建中没有资源文件夹。。。所以我在想我该怎么办?。。我在这里读过这些人的回答,他说把plist文件放在支持Files文件夹中就可以了。。这样做可以允许plist也被读取和写入吗?

要读写plist,如果您想通过没有写权限的bundle访问它,最好的做法是将它复制到文档根。我在这里提供了代码的快照,以及如何实现这一点。

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

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

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

最新更新