Plist不会复制到文档目录

时间:2010-03-22 02:09:54

标签: iphone nsfilemanager

我在xcode的资源组中有一个plist文件。我想在app启动时将其复制到我的文档目录中。我使用以下代码(取自sqlite教程):

BOOL success;
NSError *error;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"ActiveFeedsPlist.plist"];

success = [fileManager fileExistsAtPath:filePath];
if (success) return;

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"ActiveFeedsPlist.plist"];
success = [fileManager copyItemAtPath:path toPath:filePath error:&error];

if (!success) {
    NSAssert1(0, @"Failed to copy Plist. Error %@", [error localizedDescription]);
}

我收到错误“***因未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'无法复制Plist。错误操作无法完成。但是在控制台中没有这样的文件或目录'”。

知道出了什么问题吗?

由于

1 个答案:

答案 0 :(得分:10)

您错过了文件分隔符:

... stringByAppendingString:@"/ActiveFeedsPlist.plist"];

或者更好,使用:

... stringByAppendingPathComponent:@"ActiveFeedsPlist.plist"];
相关问题