noshery
noshery

Reputation: 3

Moving a file in Swift

I'm new to Swift and Xcode. Do I need to change how the path is accessed in some way using NSString or NSUrl? Problems with file path notation (file://)?

@IBAction func buttonPressed(sender: AnyObject) {        
    let filepath1 = "/Applications/Con.app/Contents/Resources/iTunes.icns"
    let filepath2 = "~/Desktop/".stringByExpandingTildeInPath
    let fileManager = NSFileManager.defaultManager()
    var error: NSError?
    fileManager.copyItemAtPath(filepath1, toPath: filepath2, error: nil)
    button1.title = "Icon changed"
 }

Upvotes: 0

Views: 71

Answers (2)

Crowman
Crowman

Reputation: 25908

You seem to be using an obsolete version of Swift, but your problem is you're not specifying a filename in your destination path. Change "~/Desktop/" to "~/Desktop/iTunes.icns", for instance. copyItemAtPath(_:toPath:) will by default fail if the destination file already exists, and "~/Desktop/" obviously does.

Upvotes: 1

john elemans
john elemans

Reputation: 2676

place the error that you defined before the copyItemAtPath in the parameter list and see what the error coming back is. ie-....error:&error) instead of error:nil)

Also add the file name to the destination string.

Upvotes: 0

Related Questions