2010-01-31 4 views
7
BOOL success; 
NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"DB"]; 
success = [fileManager fileExistsAtPath:documentDBFolderPath]; 

if (success){ 
return; 
}else{ 
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] 
             stringByAppendingPathComponent:@"DB"]; 
[fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil]; 
    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath   
                     error:&error]; 
} 
} 

Comme ceci.Copier le dossier du répertoire Ressources de l'iPhone vers le répertoire de documents

Ressources/DB/words.csv => DB dossier copie => Document/DB/words.csv

Je veux copier DB sous-répertoire dans le dossier Ressources. Je pensais que cette source est bonne. Mais cette source crée un dossier et ne copie pas les fichiers dans le dossier DB du dossier Resources. Je veux vraiment copier les fichiers dans le dossier DB dans le dossier Resources. Aidez-moi, s'il vous plaît.

Répondre

8

1) Ne pas -autorelease le NSFileManager. Vous le doublez, ce qui provoquera le blocage de votre application.

2) Pas besoin d'appeler -createDirectoryAtPath:. À partir du SDK doc de -copyItemAtPath:toPath:error:,

Le fichier spécifié dans srcpath doit exister, alors que dstPathne doit pas exister avant l'opération

et la création du répertoire de la copie à l'échec .

+0

OH !! vraiment vraiment vraiment merci !! Merci!! – Beomseok

1

Swift 3,0

aide d'une chaîne

func copyFolder(){ 

    // Get the resource folder 
    if let resourceMainPath = Bundle.main.resourcePath{ 

     var isDirectory = ObjCBool(true) 
     // Get the path of the folder to copy 
     let originPath = (resourceMainPath as NSString).appendingPathComponent("NameOfFolder") 
     // Get the destination path, here copying to Caches 
     let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first! 
     // Append the folder name to dest path so that system creates the directory if it doesnt exist 
     let destPath = (destinationPath as NSString).appendingPathComponent("/NameOfFolder") 
     let fileManager = FileManager.default 
     if fileManager.fileExists(atPath: destPath, isDirectory:&isDirectory){ 
      // If an overwrite behavior is needed, remove and copy again here 
      print("Exists") 
     }else{ 
      // Do the copy 
      do { 
       try fileManager.copyItem(atPath: originPath, toPath: destPath) 
      }catch let error{ 
       print(error.localizedDescription) 
      } 
     } 
    }else{ 

    } 

} 

Utiliser URL

func copyTheFolder(){ 

    // Get the resource folder 
    if let resourceMainURL = Bundle.main.resourceURL{ 
     var isDirectory = ObjCBool(true) 
     // Get the path of the folder to copy 
     let originPath = resourceMainURL.appendingPathComponent("NameOfFolder") 
     // Get the destination path, here copying to Caches 
     let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first! 
     // Append the folder name to dest path so that system creates the directory if it doesnt exist 
     let destURL = URL(fileURLWithPath: destinationPath).appendingPathComponent("/NameOfFolder") 
     let fileManager = FileManager.default 
     if fileManager.fileExists(atPath: destURL.path, isDirectory:&isDirectory){ 
      // If an overwrite behavior is needed, remove and copy again here 
      print("Exists") 

     }else{ 
      // Do the copy 
      do { 
       try fileManager.copyItem(at: originPath, to: destURL) 

      }catch let error{ 
       print(error.localizedDescription) 
      } 
     } 
    } 
} 
Questions connexes