I've a problem with my functions for read a local JSON file. When I want to try to print anything in my parseJson(), nothing appear in my console.
Here's my JSON file :
{
"questions":[
{
"question": "First Question ???",
"response": "First Response",
},
{
"question": "Second Question ???",
"response": "Second Response",
}
]
}
Here's my questionsResult.swift
import Foundation
struct questionsResult: Decodable {
var questions: [Questions]
}
struct Questions: Decodable {
var question: String
var response: String
}
And this is my function in my ViewController file :
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var questionsJson = [Questions]()
func parseJSON(){
if let url = Bundle.main.url(forResource: "test", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let jsonData = try decoder.decode(questionsResult.self, from: data)
questionsJson.append(contentsOf: jsonData.questions)
print(url)
} catch {
print("Json error \(error)")
}
}
}
}
}
Can you help me please ?