我想要一個文件夾中複製,它的內容在文件目錄的子目錄和它與錯誤而失敗: 「操作無法完成,沒有這樣的文件或目錄」副本OSX文件夾中的iOS目錄
首先,我嘗試在文件目錄下創建一個文件夾是這樣的:
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上的目錄複製:
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;
}
它返回「無」和我收到錯誤。
任何人有一個想法,我做錯了什麼?
要澄清一下,您試圖從Mac複製到iOS模擬器設備? – bryanmac
@bryanmac:是的。這不可能嗎? – Robert
我已更新問題標題。 – Robert