0

Need to develop a single view application with tableview getting image, name and description from a local json file. Any help would be great

1 Answers1

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
  • Error: use of unresolved identifier JSON.\ – Ayush Varshney Nov 23 '17 at 10:22
  • 1
    This 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