2014-10-29 27 views
0

我已經從應用程序中複製了像這樣的文件,並且完全複製了與其他應用程序中顯示的代碼完全相同的代碼,但由於什麼原因試圖運行此特定代碼時它只會創建新的目錄。從主包複製到應用程序支持swift

但是它不會將我保存在主包中的支持文件中的二進制文件保存到新目錄中。我做了大量的谷歌搜索和搜索堆棧溢出,並決定我可能不得不在這裏發佈一些東西。

let path = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("youtube-dl") 
let destination = "~/Library/Application Support/Youtube DLX/" 
let destinationPath = destination.stringByStandardizingPath 

NSFileManager.defaultManager().createDirectoryAtPath(destinationPath, withIntermediateDirectories: true, attributes: nil, error: nil) 
NSFileManager.defaultManager().copyItemAtPath(path!, toPath: destinationPath + "youtube-dl", error: nil) 

請注意,我試圖複製的文件沒有擴展等等全名就是「YOUTUBE-DL」

+0

請不要忽略錯誤的參數。這是有原因的。 – 2014-10-29 06:36:38

回答

1

let destinationPath = destination.stringByStandardizingPath 

結果沒有結尾的斜線,所以你試圖將文件複製到

"~/Library/Application Support/Youtube DLXyoutube-dl" 

您應該使用

destinationPath.stringByAppendingPathComponent("youtube-dl") 

以與源路徑相同的方式生成目標路徑。

一般(如剛剛在留言中提到),你應該使用錯誤的參數和 檢查成功的返回值,例如

var error : NSError? 
if !NSFileManager.defaultManager().copyItemAtPath(..., error: &error) { 
    println("copy failed: \(error)") 
} 
相關問題