I'm appending data to a JSON object which works and prints out the newly appended data. However, I can't save it to a local .json file within my Xcode project.
do {
let url = Bundle.main.url(forResource: "police_stations", withExtension: "json")!
let data = try Data(contentsOf: url)
let jsonDecoder = JSONDecoder()
var parsedJSON = try jsonDecoder.decode([UserCoords].self, from: data)
parsedJSON.append(Vac_Pass.UserCoords(lat: latitude, lng: longitude))
let wer = try JSONEncoder().encode(parsedJSON)
// prints data out
let json = try JSONSerialization.jsonObject(with: wer, options: [])
print(json)
let file = "police_stations.json"
let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent(file)
try wer.write(to: fileURL!)
print("ran up to this point")
} catch {
print(error)
}
"police_stations.json" file looks like this:
[
{"lat" : 38.1149, "lng" : 145.173 } ,
{"lat" : 38.0315, "lng" : 143.633 } ,
{"lat" : 38.0572, "lng" : 147.569 }
]
Thank you - all inputs are appreciated :)