如果您想通过Bundle访问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文件放在任何位置。
重要的是将其复制到bundle中。 因此,为了确保这一点,请检查
项目设置>构建阶段>复制包资源
您可以通过在项目导航器中左键单击项目来打开项目设置。