0

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
}
Pieter Bikkel
  • 97
  • 1
  • 8
  • Add `task.resume()` before the last return – Joakim Danielson Apr 28 '20 at 11:52
  • @JoakimDanielson I'm not an expert in asynchronous work, but wouldn't that not work either, because the array is being returned empty before the request finishes? – Sam Apr 28 '20 at 11:54
  • I got his error message: "Thread 2: signal SIGABRT" at this line: "let array = json as! NSArray". This was printed to the console: "Could not cast value of type '__NSDictionaryI' (0x7fff87a60a28) to 'NSArray' (0x7fff87a60528). 2020-04-28 13:53:54.199539+0200 Competitie[1653:111335] Could not cast value of type '__NSDictionaryI' (0x7fff87a60a28) to 'NSArray' (0x7fff87a60528)." – Pieter Bikkel Apr 28 '20 at 11:55
  • How do I solve this casting issue? – Pieter Bikkel Apr 28 '20 at 11:58
  • The error message clearly says that the received object is a dictionary. By the way don't use `NSArray`, `NSDictonary` and `value(forKey` in Swift. – vadian Apr 28 '20 at 11:58
  • My comment was in regard to the compiler warning and to get the task running, I did not pay any attention to the code in the closure – Joakim Danielson Apr 28 '20 at 11:59
  • How should I do it the proper way @vadian? Can you improve my code? I am new with swift. – Pieter Bikkel Apr 28 '20 at 12:00
  • Please learn to read JSON. It's pretty easy. The root object is a dictionary ( `{}` -> `[String:Any]`), in there is an other dictionary for key `results`, in there is the array (`[]` -> `[[String:Any]]`) for key `competition` you are looking for. – vadian Apr 28 '20 at 12:06
  • @vadian do you have a video recommendation for me? – Pieter Bikkel Apr 28 '20 at 12:08
  • let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary guard let jsonDict = json, let resultsDict = jsonDict["results"] as? NSDictionary, let resultsArray = resultsDict["competition"] as? NSArray else { return } // use the resultArray in your for loop – Durga Vundavalli Apr 28 '20 at 12:24

0 Answers0