Need to develop a single view application with tableview getting image, name and description from a local json file. Any help would be great
Asked
Active
Viewed 1,597 times
0
-
In Swift 4 you can use `JSONDecoder` – vadian Nov 23 '17 at 10:20
1 Answers
0
To get data from local json file use below lines of code
if let path = Bundle.main.path(forResource: "countries", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
//parse data here
//If you are using SwiftyJSON use below commanded line
// let jsonObj = try JSON(data: data)
} catch let error {
print("parse error: \(error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
}
Hope that will help you

Ganesh Manickam
- 2,113
- 3
- 20
- 28
-
-
1This will not compile if SwiftyJSON is not installed (not everybody uses it and it's obsolete in Swift 4 in many cases) and why do you create the file URL although there is `url(forResource` in `Bundle`? – vadian Nov 23 '17 at 10:22
-
If your are using SwiftyJSON you can use that commanded line otherwise parse it into JSON data @Ayush – Ganesh Manickam Nov 23 '17 at 10:33