源路径为零 - 但是plist是捆绑资源

时间:2013-09-18 21:53:42

标签: ios objective-c xcode

我收到“source path is nil”错误,但是应该位于源路径的plist是在bundle资源中。

代码:

{NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"gHaiku413.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"gHaiku413" ofType:@"plist"];
            NSLog(@"%@",bundle);
    [fileManager copyItemAtPath:bundle toPath: path error:&error];

}

当我进入Build Phases并查看Copy Bundle Resources时,gHaiku413.plist肯定存在,但我得到的结果是:

//This is the output of NSLog(@"%@",bundle):
2013-09-18 16:49:22.728 GHaiku[39389:a0b] (null)
2013-09-18 16:49:22.733 GHaiku[39389:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil'

我错过了什么?

1 个答案:

答案 0 :(得分:0)

不确定这只是您问题中的拼写错误或实际问题,但[[NSBundle mainBundle] pathForResource:@"gHaiku413" ofType:@"plist"];查找gHaiku413.plist并且您说文件gHaiku.plist肯定存在。这些是不同的文件名。

更新:经过评论讨论后,原来是:

  1. 尝试一个非常干净的构建:cmd⌘ + alt⌥ + Shift⇧ + K 并且失败这是一个超级干净的构建:
    • 关闭Xcode
    • 删除xcuserdata个目录:MyProject.xcodeproj/xcuserdataMyProject.xcodeproj/project.xcworkspace/xcuserdata
    • 删除~/Library/Developer/Xcode/DerivedData目录
    • 重启Xcode
    • 构建
  2. 重置模拟器中的内容和设置
相关问题