How to read any of the above mentioned file via Xcode in swift 4 language.
Asked
Active
Viewed 138 times
-2
-
1By doing some research. – Joakim Danielson Apr 25 '19 at 12:37
-
You should be able to Google a solution to this. If after that you are still having issues you can post the problematic code and someone will probably be happy to help you debug and fix the issue(s). – Vasil Dininski Apr 25 '19 at 12:44
1 Answers
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".

Hadi
- 109
- 10