2016-05-21 10 views
0

OS XでSwiftを使用してフォルダの内容全体をコピーして貼り付けるにはどうすればよいですか? destinationPathに既にフォルダが含まれている場合は、置き換える必要があります。Swiftを使用してファイルシステムのフォルダを置き換えます

私は

let appSupportSourceURL = NSURL(string: appSupportSourcePath) 
     let appSupportDestinationURL = NSURL(string: appSupportDestinationPath+"/"+appSupportFileName) 

     if (fileManager.isReadableFileAtPath(appSupportSourcePath)){ 
      do { 
      try fileManager.copyItemAtURL(appSupportSourceURL!, toURL: appSupportDestinationURL!)} 
      catch{ 
      } 
     } 

を試してみましたが、私はこれが唯一のファイルに対して動作することを実現。私はフォルダ全体を交換しようとしています。

答えて

1

Appleは、ファイルシステムのパスを指定するURLを使用する新しいコードを推奨しています。しかし、NSFileManagerは古いクラスであり、古い文字列ベースのパスと新しいURLベースのパラダイムの間で遷移しています。 NSURL

let appSupportSourceURL = NSURL(fileURLWithPath: "...", isDirectory: true) 
let appSupportDestionURL = NSURL(fileURLWithPath: "...", isDirectory: true) 

try! NSFileManager.defaultManager().copyItemAtURL(appSupportSourceURL, toURL: appSupportDestionURL) 
で方法:

let appSupportSourcePath = "..." 
let appSupportDestinationPath = "..." 

let fileManager = NSFileManager.defaultManager() 
do { 
    // Delete if already exists 
    if fileManager.fileExistsAtPath(appSupportDestinationPath) { 
     try fileManager.removeItemAtPath(appSupportDestinationPath) 
    } 
    try fileManager.copyItemAtPath(appSupportSourcePath, toPath: appSupportDestinationPath) 

} catch { 
    print(error) 
} 

編集:これを試してみてください

関連する問題