我試圖用[[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