copy a json file in swift

To copy a JSON file in Swift, you can use the FileManager class and its copyItem function. Here's an example code snippet:

main.swift
import Foundation

let fileManager = FileManager.default
let sourceURL = URL(fileURLWithPath: "/path/to/source.json")
let destinationURL = URL(fileURLWithPath: "/path/to/destination.json")

do {
    try fileManager.copyItem(at: sourceURL, to: destinationURL)
} catch {
    print("Error copying file: \(error)")
}
313 chars
12 lines

In this example, sourceURL is the URL of the JSON file you want to copy, and destinationURL is the URL of the destination file where you want to copy the JSON file. The FileManager.default instance is used to perform the copy operation.

You can adjust the file paths to match your specific use case. Additionally, make sure to handle any errors that may occur during the copy operation.

related categories

gistlibby LogSnag