copy a csv file in swift

To copy a csv file in Swift, you can use the FileManager class which provides methods to perform file-related operations. Here's an example code snippet to copy a csv file:

main.swift
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let sourceURL = documentsDirectory.appendingPathComponent("source.csv")
let destinationURL = documentsDirectory.appendingPathComponent("destination.csv")

do {
    try fileManager.copyItem(at: sourceURL, to: destinationURL)
    print("CSV file copied successfully.")
} catch let error {
    print("Error while copying CSV file: \(error.localizedDescription)")
}
495 chars
12 lines

In the code above, we first get the URL of the documents directory where we will be storing our csv files. Then we create two URLs, one for the source csv file and one for the destination csv file. Finally, we use the copyItem(at:to:) method of FileManager to copy the source csv file to the destination csv file. Any errors that may occur during the file copy process are caught in the catch block.

gistlibby LogSnag