【问题标题】:Plist won't copy to documents directoryPlist不会复制到文档目录
【发布时间】:2011-01-30 04:26:53
【问题描述】:

我在 xcode 的资源组中有一个 plist 文件。我正在尝试在应用启动时将其复制到我的文档目录中。我正在使用以下代码(取自 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。错误操作无法完成。没有这样的文件或目录'”。

知道有什么问题吗?

谢谢

【问题讨论】:

    标签: iphone nsfilemanager


    【解决方案1】:

    您缺少文件分隔符:

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

    或者,更好的是,使用:

    ... stringByAppendingPathComponent:@"ActiveFeedsPlist.plist"];
    

    【讨论】:

    • 或 stringByAppendingPathComponent:@"ActiveFeedsPlist.plist"
    • 在这种情况下,最好使用-stringByAppendingPathComponent:。 (D'oh,被打败了一分钟!)
    • @dbarker,伟大的思想!哈哈,在你发表评论的同时,我正在编辑它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2015-03-28
    • 1970-01-01
    • 2012-11-30
    相关资源
    最近更新 更多