2014-10-29 21 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)") 
}