【发布时间】:2012-10-06 02:28:46
【问题描述】:
我正在与一个团队一起开发,使用的 Mac 的映像都完全相同。使用我编写代码的机器,我可以以编程方式创建然后写入 pList 文件。但是当我使用我的家用机器(相同的图像)或当我的队友在他们的机器上测试代码时,我们会因 [NSFileManager copyItemAtPath:toPath:error:]: source path is nil' 错误而崩溃。在调试中,文件路径是有效的。但是,该捆绑包位于:
NSString *bundle = [[NSBundle mainBundle]pathForResource:@"Family" ofType:@"plist"];
[pListfileMgr copyItemAtPath:bundle toPath: pListpath error:&error];
为零。我们正在使用 git 对我们的代码进行版本控制。我们一直将 .DS_Store 和 /userdata/ 文件排除在存储库之外,除非我遗漏了什么。到底是怎么回事?这是代码,几乎是从here逐字复制的:
NSError *error;
NSArray *pListpaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *pListdocumentsDirectory = [pListpaths objectAtIndex:0];
NSString *pListpath = [pListdocumentsDirectory stringByAppendingPathComponent:@"Family.plist"];
NSFileManager *pListfileMgr = [NSFileManager defaultManager];
//Create a plist if it doesn't alread exist
if (![pListfileMgr fileExistsAtPath: pListpath])
{
NSString *bundle = [[NSBundle mainBundle]pathForResource:@"Family" ofType:@"plist"];
[pListfileMgr copyItemAtPath:bundle toPath: pListpath error:&error];
}
NSMutableDictionary *thePList = [[NSMutableDictionary alloc] initWithContentsOfFile: pListpath];
[thePList setObject:[NSString stringWithFormat:@"%@", numberOfPeopleInFamilyOfOrigin] forKey:@"famSize"];
[thePList writeToFile: pListpath atomically: YES];
【问题讨论】:
-
仍在寻求答案。我只是想强调,如果这个 plist 文件不存在,我们希望能够创建它。它主要用于编写 TO,而不是手动创建以读取静态值以填充表格或其他一些字段。如果我理解了,下面的@Olaf 说我应该确保文件资源正在复制到我队友的包中。这不是我的目标(除非我错了,不知道我在说什么)。
-
更新:我从我的模拟器中删除了该应用程序并运行了一个新版本。现在我机器上的项目抛出了同样的错误。
标签: ios file-io nsfilemanager