我试图用[[NSFileManager defaultManager] copyItemAtPath: toPath: error:]
复制一个文件不存在的文件,但它与下面的错误而失败:的NSFileManager copyItemAtPath抱怨确实存在
4: The file does not exist.
相关的代码如下,并且该文件确实存在,路径字符串是正确的,因为它是事先用完全相同的文件路径字符串创建的。
NSFileManager* manager = [NSFileManager defaultManager];
NSError* error;
NSString* fileName = [Sound getFileName:Title];
NSString* oldDirectory = [NSString stringWithFormat:@"%@%@/", [settings stringForKey:@"downloadFolder"], authorFolder];
NSString* oldFile = [oldDirectory stringByAppendingFormat:@"%@.mp3", fileName];
NSString* newFile = [NSString stringWithFormat:@"%@/iTunes/iTunes Media/Automatically Add to iTunes/%@.mp3", [NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES) objectAtIndex:0], fileName];
BOOL result = [manager copyItemAtPath:oldFile toPath:newFile error:&error];
if (!result && error)
{
NSLog(oldFile);
NSLog(@"There was an error copying the file to the iTunes directory! %@", [error localizedDescription]);
}
这不是确切的代码,但所有相关的代码应该在上面。如果我使用[manager fileExistsAtPath:oldFile]
,结果是是。
什么会导致复制失败,并说该文件不存在,即使它呢?
UPDATE:
发出定额。结果是输出文件夹实际上是自动添加到iTunes.localized,但是当通过查找器分页时,我没有注意到这一点。修复输出路径解决了问题!谢谢您的帮助。
记住了'copyItemAtPath:toPath:错误:'方法,如果目标文件已经存在失败。它不会覆盖现有的文件。 – rmaddy 2013-03-20 06:15:46
@rmaddy感谢你的抬头。我已在本地更正了我的代码以检查此问题,但我已手动确保目标文件不存在,并且仍然收到此错误。 – 2013-03-20 06:35:12