0

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 :)

Alexander Choi
  • 101
  • 1
  • 1
  • 6

2 Answers2

0

Replace

let path = Bundle.main.path(forResource: "police_stations", ofType: "json")
let url = URL(fileURLWithPath: path!)

URLSession.shared.dataTask(with: url) { (data, response, error) in

    if let data = data {

including the trailing resume() with

let url = Bundle.main.url(forResource: "police_stations", withExtension: "json")!
let data = try Data(contentsOf: url)

and put it into the do scope. Loading data asynchronously from the file system is pointless.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thank you for the help, but the "police_stations.json" file is still not loading the new appended data. I updated my code above in case anything I did was wrong. – Alexander Choi Aug 09 '21 at 13:25
  • Consider that the file in the bundle is not being modified. To prove the changes you have to read back the file in `Documents`. – vadian Aug 09 '21 at 13:40
0

Looks like you're expecting to change json which is included into your xcode project.

You can't do it from the application code. When the app is built, all resources, including json files gets copied into the app container. And your app doesn't have access to your source code.

You can modify the source codes using "Run script phase" scripts

If this still doesn't help, please update your question by describing your intention more clearly. When do you need this code to be run?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
  • Yes, that is what I am trying to do. However, Im still fairly new to swift, so Im not sure how to implement Run script phase scripts but I appreciate your help – Alexander Choi Aug 09 '21 at 23:37
  • @AlexanderChoi check out [What is the Run Script](https://stackoverflow.com/q/39632301/3585796). It'll we run on any build you made(or you can select checkbox "For install builds only"). By default it runs a bash script, you you can change `/bin/sh` to `/usr/bin/env xcrun --sdk macosx swift` to use swift. As you'll have output files, you need to check [this answer too](https://stackoverflow.com/a/68616914/3585796). There're a lot to learn for you. It's hard to describe everything you may need, especially without knowing your purpose, like do you really need to run this script for each build? – Phil Dukhov Aug 10 '21 at 03:38