jOakes90
jOakes90

Reputation: 5

copy from main bundle to application support swift

I have copied files like this from an application before and have copied the code exactly as it appears in another app but for what ever reason when it try to run this particular code it will only create the new directory.

It will not however save the binary file I have saved in supporting files in the main bundle to the new directory. I have done a fair amount of googling and searching stack overflow and have decided I might have to post something here.

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)

note that the file that I am trying to copy has no extension so the full name is just "youtube-dl"

Upvotes: 0

Views: 1521

Answers (1)

Martin R
Martin R

Reputation: 539745

The result of

let destinationPath = destination.stringByStandardizingPath

has no trailing slash, so you are trying to copy the file to

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

You should use

destinationPath.stringByAppendingPathComponent("youtube-dl")

to generate the target path as you already did for the source path.

Generally (as just mentioned in a comment), you should use the error parameter and check the return value for success, for example

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

Upvotes: 2

Related Questions