move file in swift

To move a file in Swift, you can use the NSFileManager class. Here is an example code snippet that shows how to move a file from one location to another:

main.swift
let fileManager = NSFileManager.defaultManager()
let sourcePath = "/path/to/source/file"
let destinationPath = "/path/to/destination/file"

do {
    try fileManager.moveItemAtPath(sourcePath, toPath: destinationPath)
    print("File moved successfully")
} catch let error {
    print("Error: \(error.localizedDescription)")
}
326 chars
11 lines

In the above code, you need to specify the paths of the source file and the destination file. The moveItemAtPath(_:toPath:) method of NSFileManager is then called to move the file. If the file is successfully moved, then the message "File moved successfully" will be printed. Otherwise, an error message will be printed.

gistlibby LogSnag