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

Như thế này.Sao chép thư mục từ thư mục Tài nguyên iPhone vào thư mục tài liệu

Resources/DB/words.csv => DB thư mục bản sao => Tài liệu/DB/words.csv

Tôi muốn sao chép thư mục con DB tại thư mục Resources. Tôi nghĩ rằng nguồn là tốt. Nhưng nguồn đó tạo thư mục và không sao chép các tệp trong thư mục DB tại thư mục Tài nguyên.

Tôi thực sự muốn sao chép tệp trong thư mục DB tại thư mục Tài nguyên. làm ơn giúp tôi.

Trả lời

8

1) Không -autoreleaseNSFileManager. Bạn đang phát hành gấp đôi ứng dụng sẽ làm hỏng ứng dụng của bạn.

2) Không cần gọi số -createDirectoryAtPath:. Từ doc SDK của -copyItemAtPath:toPath:error:,

Hồ sơ quy định tại srcPath phải tồn tại, trong khi dstPathkhông phải tồn tại trước đến hoạt động

và tạo ra các thư mục bản sao để thất bại .

+0

OH !! thực sự thực sự cảm ơn !! cảm ơn!! – Beomseok

1

Swift 3,0

Sử dụng chuỗi

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{ 

    } 

} 

Sử dụng 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) 
      } 
     } 
    } 
} 
Các vấn đề liên quan