0

How can I call the below function in a different swift (view) file so that print statement (the one not in the comments (*/)) displays?

I have tried passing the function into a variable within the new view but this has not worked.

Would it also be easier to remove "do" statement entirely?

 func getJSON(){
        guard let url = URL(string: "https://raw.githubusercontent.com/owid/covid-19-data/68c39808d445fe90b1fe3d57b93ad9be20f796d2/public/data/latest/owid-covid-latest.json") else{
            return
        }
        let request = URLRequest(url: url)
        URLSession.shared.dataTask(with: request){ (data, response, error) in
            if let error = error{
                print(error.localizedDescription)
                return
            }
            guard  let data = data else{
                return
            }
            let decoder = JSONDecoder()
            do {
                //let decodedData = try decoder.decode([String:Country].self, from: data)
                //print (decodedData)
                let res = try decoder.decode([String:Country].self, from: data)
                let locations = Array(res.values).map { $0.location}
                let caseNumbers = Array(res.values).map{ $0.numberCheck}
                let vaccineNumbers = Array(res.values).map{ $0.vaccineCheck}
                
                /*Dual for loop
                 for (x, y) in zip(locations, caseNumbers) {
                    print("Country: ", x, " \t\t Yesterdays case numbers: ", y, ".")
                }*/
                
                ***for item in 0..<locations.count{
                    print("Country: ", locations[item], "\nCase Numbers 24hrs: ", caseNumbers[item], "\nNumber of People Fully Vaccinated: ", vaccineNumbers[item],"\n\n")***
                    
                    //Potential use in App when certain country is selected
                    //if locations[item] == "United Kingdom" {
                        //print(caseNumbers[item])}
                }
            }
            catch {
                print(error)
            }
        }.resume()
    }
4
  • Not sure what you are asking here. What stops you from calling this function from another object and what do you mean with "print statement displays", do you want to use the result in your view? Commented Apr 17, 2021 at 15:14
  • I get the error Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols when calling the function in another view. I just want whats included in the print statement of the function to display in the new view Commented Apr 17, 2021 at 15:36
  • Apparently you are talking about SwiftUI. This information is crucial. You need the Observable-/ObservedObject pattern Commented Apr 17, 2021 at 16:25
  • Does this answer your question? Display elements after parsing Commented Apr 17, 2021 at 16:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.