I want to replace the contents of a JSON file with data coming from an API call. I have tried the following:
let filePath: String = Bundle.main.path(forResource: "shared", ofType: "json")!
let fileUrl: URL = URL(fileURLWithPath: filePath)
do {
try jsonData.write(to: fileUrl, atomically: false, encoding: .utf8)
do {
let newData = try String(contentsOf: fileUrl, encoding: .utf8)
print("New Data: \(newData)")
} catch {
print("Failed to read updated file data")
}
} catch {
print("Failed to write data to file")
}
I've confirmed that "shared.json" is listed under "Copy Bundle Resources". I'm not getting any errors. Yet the contents of "shared.json" never changes. What am I doing wrong?