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

像这样。将文件夹从iPhone Resources目录复制到文档目录

资源/ DB/words.csv => DB文件夹拷贝=>文件/数据库/ words.csv

我想DB子目录资源文件夹复制。我认为这个来源是很好的。但是该源生成文件夹,并且不会将资源文件夹中DB文件夹中的文件复制。

我真的想复制资源文件夹中DB文件夹中的文件。请帮帮我。

回答

8

1)不要-autoreleaseNSFileManager。你是双释放它会导致你的应用程序崩溃。 2)不需要拨打电话-createDirectoryAtPath:。从-copyItemAtPath:toPath:error:的SDK文档,

srcPath必须存在指定的文件,而dstPath必须不存在操作

,并创建目录拷贝失败之前, 。

+0

哦!真的非常感谢!谢谢!! – Beomseok 2010-01-31 10:15:34

1

雨燕3.0

使用字符串

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{ 

    } 

} 

使用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) 
      } 
     } 
    } 
} 
相关问题