My goal is to add the parsed JSON to my struct so that I can call it in my tableviewcell. But I got stuck at this warning message: Initialization of immutable value 'task' was never used; consider replacing with assignment to '_' or removing it
And my jsonParser.count returns zero so that's not right. How can I fix it so that my struct gets filled with the data from the json results. Before I added the return void of jsonParser() I could print the json just fine, now it's empty.
This is my code
func jsonParser() -> [SearchResults] {
let urlString: String
var ret = [SearchResults]()
//https://www.volleybal.nl/xhr/search.json?q=Dash&type=competition&_=1587407420006
urlString = "https://www.volleybal.nl/xhr/search.json?q=\(search)&type=competition&_=1587407420006"
let url = URL(string: urlString)!
let session = URLSession.shared
let task = session.dataTask(with: url) { (data, response, error) in
guard let data = data else {
print("Data could not be found")
return
}
let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
let array = json as! NSArray
for obj in array {
if let dict = obj as? NSDictionary {
let team = dict.value(forKey: "title")
let url = dict.value(forKey: "url")
let nieuws = SearchResults(team: team as! String, url: url as! String)
ret.append(nieuws)
}
}
}
return ret
}
This is my struct
struct SearchResults {
var team: String
var url: String
}