To delete a JSON file in Swift, we can use the FileManager
class provided by Apple.
Here's an example code snippet that shows how to delete a JSON file named "example.json" that is located in the documents directory of the app:
main.swift568 chars16 lines
In this code snippet, we first get the URL to the app's documents directory using FileManager.default.urls(for:in:)
and then append the name of the JSON file to it using appendingPathComponent(_:)
.
We then check if the file exists using FileManager.default.fileExists(atPath:)
, and if it does, we try to delete it using FileManager.default.removeItem(at:)
. If the deletion is successful, we print a message confirming that the file was deleted. If an error occurs during the deletion process, we print the error message to the console.
Note: Make sure to handle any errors that may occur during the file deletion process to prevent the app from crashing or behaving unexpectedly.
gistlibby LogSnag