2012-10-30 31 views
0

我想要一個文件夾中複製,它的內容在文件目錄的子目錄和它與錯誤而失敗: 「操作無法完成,沒有這樣的文件或目錄」副本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; 
} 

它返回「無」和我收到錯誤。

任何人有一個想法,我做錯了什麼?

+1

要澄清一下,您試圖從Mac複製到iOS模擬器設備? – bryanmac

+0

@bryanmac:是的。這不可能嗎? – Robert

+0

我已更新問題標題。 – Robert

回答

1

的裝置(並且因此仿真器)是從操作系統,所以你不能直接做文件系統複製隔離。想象一下,即使它可以讓你從模擬器中執行它,運行你的應用程序的斷開設備如何訪問操作系統文件系統?

您將不得不尋找其他選項,例如在mac上有一個應用程序,該應用程序在設備從該設備複製的mac上具有opens sockets或具有http端點。其他選項包括同步文檔via iCloudanother cloud service。你也可以transfer files via iTunes。我敢肯定,還有很多其他選項...同時結賬this

+0

謝謝。我是iOS編程的新手,對此一無所知。 – Robert

+1

np - 也結帳:http://stackoverflow.com/questions/7913342/transfer-file-from-mac-pc-to-iphone-app-document-folder – bryanmac