【问题标题】:copy from main bundle to application support swift从主包复制到应用程序支持 swift
【发布时间】:2014-10-29 06:18:02
【问题描述】:

我之前从一个应用程序中复制了这样的文件,并且复制了与另一个应用程序中显示的完全相同的代码,但是无论出于何种原因,当它尝试运行此特定代码时,它只会创建新目录。

但它不会将我保存在主包中的支持文件中的二进制文件保存到新目录中。我已经进行了大量的谷歌搜索和搜索堆栈溢出,并决定我可能必须在这里发布一些内容。

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”

【问题讨论】:

  • 请不要忽略错误参数。这是有充分理由的。

标签: macos cocoa swift


【解决方案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)")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-25
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2020-01-31
    相关资源
    最近更新 更多