I have a local json file, to read what I do as follows:
func readJson() {
if let path = Bundle.main.path(forResource: "text", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let decoder = JSONDecoder()
let model = try decoder.decode(Textos.self, from: data)
print("model", model)
} catch {
print("error file")
}
} else {
print("error")
}
}
the read function works fine but when I want to update the file it doesn't update.
func saveJson(response: Textos) {
do {
let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(response) //all fine with jsonData here
let json = String(data: jsonData, encoding: String.Encoding.utf8)
let data = Data(json!.utf8)
if let path = Bundle.main.path(forResource: "text", ofType: "json") {
do {
try? data.write(to: URL(fileURLWithPath: path)) **//not Working**
readJson()
}
}
} catch {
print(error)
}
}
the saveJson function works fine in a simulator but when I test it with a device it doesn't work.