我正在尝试将文件夹及其内容复制到文档目录中的子目录中,但失败并出现错误:“操作无法完成。没有这样的文件或目录”
首先,我尝试在文档目录中创建一个文件夹,如下所示:
NSString *diagramsDirectory = [docDirectory stringByAppendingPathComponent:@"Diagrams"];
if (![fileManager fileExistsAtPath:docDirectory isDirectory:&isDirectory] || !isDirectory)
{
NSError *error = nil;
NSDictionary *attr = [NSDictionary dictionaryWithObject:NSFileProtectionComplete
forKey:NSFileProtectionKey];
[fileManager createDirectoryAtPath:diagramsDirectory
withIntermediateDirectories:NO
attributes:attr
error:&error];
if (error) {
NSLog(@"error creating dir. path: %@", [error localizedDescription]);
}
}
NSLog(@"diagrams directory = %@", diagramsDirectory);
控制台日志似乎表明这是可行的:
diagrams directory = /Users/../iPhone Simulator/../Library/Documentation/Diagrams
但是,当我尝试从 Mac 上的目录中复制一个名为“Diagrams”的文件夹时:
NSString *pathToDirectories = @"/User/Desktop/Project Resource Files/Files/";
NSError *error = nil;
NSArray *folders = [fileManager contentsOfDirectoryAtPath:pathToDirectories error:&error];
for (NSString *folder in folders) {
if ([folder isEqualToString:@"Diagrams"]) {
[self copyFolderAtPath:folder toDestinationFolderAtPath:docDirectory];
}
它调用“copyFolderAtPath”方法:
- (BOOL)copyFolderAtPath:(NSString *)sourceFolder toDestinationFolderAtPath:(NSString *)destinationFolder
{
destinationFolder = [destinationFolder stringByAppendingPathComponent:[sourceFolder lastPathComponent]];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
// check for destination folder
if ([fileManager fileExistsAtPath:destinationFolder])
{
if (![fileManager removeItemAtPath:destinationFolder error:&error])
{
NSLog(@"Could not remove old files. Error: %@", error);
return NO;
}
}
error = nil;
// copy destination
if (!([fileManager copyItemAtPath:sourceFolder toPath:destinationFolder error:&error])) {
NSLog(@"failed copying file at path %@ to path %@. Error %@", sourceFolder, destinationFolder, error);
return NO;
}
return YES;
}
它返回“否”,我得到错误。
有人知道我在做什么错吗?