尝试复制文件时出错
我想使用NSFileManager复制临时文件到另一个位置。 然而,这是失败的抱怨,其中一个文件不存在。
//Copy temp file NSError *error; BOOL exists = [fileManager fileExistsAtPath:chapterFileTemp]; exists = [fileManager fileExistsAtPath:chapterFile]; [fileManager copyItemAtURL:[NSURL fileURLWithPath:chapterFileTemp] toURL:[NSURL fileURLWithPath:chapterFile] error:&error]; //Delete temp path [fileManager removeItemAtURL:[NSURL fileURLWithPath:chapterFileTemp] error:&error];
我在复制操作时出错
(错误域= NSCocoaErrorDomain代码= 260“操作无法完成(Cocoa错误260.)”UserInfo = 0x1c5190b0 {NSFilePath = / var / mobile / Applications / 57727CCD-88AD-4D84-8C78-EA8100645C9B / Documents / 119 /myFileTemp.temp,NSUnderlyingError = 0x1c527960“操作无法完成,没有这样的文件或目录”)。
现在第一个BOOL返回YES,第二个NO。 这是预期的。
什么可能是失败的原因
谢谢。
当目标path缺less目录时,这是可能的。 确保您要复制源文件的位置包含所有必需的目录。
如果,正如+ Apurv指出的,目标目录不存在,那么你应该做两件事情:
- 确保您的目标位置
chapterFile
创build在您有权访问的位置。 iOS应用程序在写入文件的地方受到限制; 您需要创build一个与iOS应用程序限制一致的path。 请参阅URLsForDirectory:inDomains
。 - 调用
createDirectoryAtPath:withIntermediateDirectories:attributes:error:
创build目标目录。
在这两件事情之后, copyItem
方法应该工作。
从NSFileManager
使用
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
createIntermediates
为YES
。