-2

How to read any of the above mentioned file via Xcode in swift 4 language.

1 Answers1

0

Here's how to read a local .json file named "test.json":

if let path = Bundle.main.path(forResource: "test", ofType: "json") {
    do {
          let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
          let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
          if let jsonResult = jsonResult as? Dictionary<String, AnyObject>, let person = jsonResult["person"] as? [Any] {
                    // do stuff
          }
      } catch {
           // handle error
      }
}

Where "test" is the name of the file and "json" is type. You can replace ofType: argument to the type you want e.g. "txt", "xml".

Source: Reading in a JSON File Using Swift

Hadi
  • 109
  • 10