我正在尝试使用复制文件,[[NSFileManager defaultManager] copyItemAtPath: toPath: error:]
但失败并出现以下错误:
4:文件不存在。
相关代码如下,该文件确实存在并且路径字符串是正确的,因为它是使用完全相同的文件路径字符串预先创建的。
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]
结果是YES。
什么可能导致复制失败并说文件不存在,即使它存在?
更新:
问题已修复。原来输出文件夹真的是自动添加到 iTunes.localized,但我最初在浏览 finder 时并没有注意到这一点。修复输出路径解决了这个问题!谢谢您的帮助。