find the path of a json file in swift

To find the path of a JSON file in Swift, you can use the Bundle class to get the path of the file within your app's bundle. Here is an example code snippet:

main.swift
if let path = Bundle.main.path(forResource: "filename", ofType: "json") {
    // use the `path` variable to load the JSON file
} else {
    print("File not found")
}
166 chars
6 lines

Replace "filename" with the name of your JSON file (without the extension) and "json" with the extension of your JSON file (if different). The path(forResource:ofType:) method returns the file path if the file is found in the app's bundle, which you can then use to load the file.

related categories

gistlibby LogSnag