2010-01-31 21 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]; 
} 
} 

Me gusta.Copiar carpeta del directorio de recursos de iPhone al directorio de documentos

Recursos/DB/words.csv => DB copia de carpeta => Documento/DB/words.csv

quiero copiar subdirectorio DB en la carpeta Recursos. Pensé que esa fuente es buena. Pero esa fuente crea la carpeta y no copia los archivos en la carpeta DB en la carpeta de Recursos.

Realmente quiero copiar archivos en la carpeta DB en la carpeta Resources. por favor, ayúdame.

Respuesta

8

1) No -autoreleaseNSFileManager. Lo está liberando dos veces, lo que bloqueará su aplicación.

2) No es necesario llamar al -createDirectoryAtPath:. Desde el doc SDK de -copyItemAtPath:toPath:error:,

El archivo especificado en SRCPATH debe existir, mientras dstPathno debe existir antes de la operación

y crear el directorio de la copia a fallar .

+0

OH !! realmente, realmente, muchas gracias! ¡¡Gracias!! – Beomseok

1

Swift 3,0

El uso de cuerdas

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{ 

    } 

} 

El uso de 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) 
      } 
     } 
    } 
} 
Cuestiones relacionadas