func fetchCountryData(completion: @escaping ([Country]?) -> Void) { guard let url = URL(string: "https://restcountries.com/v3.1/all") else { print("Invalid URL") completion(nil) return } URLSession.shared.dataTask(with: url) { data, response, error in guard let data = data, error == nil else { print("Error: \(error?.localizedDescription ?? "Unknown error")") completion(nil) return } // Print the raw JSON data for inspection if let jsonString = String(data: data, encoding: .utf8) { print("Raw JSON Data: \(jsonString)") } do { let countries = try JSONDecoder().decode([Country].self, from: data) completion(countries) } catch { print("Error decoding JSON: \(error.localizedDescription)") completion(nil) } }.resume() } // Usage fetchCountryData { countries in if let countries = countries { for country in countries { print("Name: \(country.name)") print("Languages: \(country.languages.joined(separator: ", "))") print("Flag URL: \(country.flags["svg"] ?? "")") print("------") } } else { print("Failed to fetch country data.") } }