I'm working on a weather app where I type in the name of the city in the text field, the app then hits the Open Weather Map API, and returns the weather for that location. It works if the city name is only one word, like Chicago, but a city with two words in it, such as Los Angeles, doesn't work.
I'm new to programming and my understanding is that to indicate a space in a URL, the space needs to be represented as "%20".
Checking it in my browser, adding "&q=Los%20Angeles" to the end of the URL works.
What's the best way to incorporate the possibility of a space being entered in a name in the text field, and then formatting that string w/a space into a URL?
I'm new to programming and my understanding is that to indicate a space in a URL, the space needs to be represented as "%20".
Checking it in my browser, adding "&q=Los%20Angeles" to the end of the URL works.
What's the best way to incorporate the possibility of a space being entered in a name in the text field, and then formatting that string w/a space into a URL?
Code Block import UIKit import CoreLocation protocol APIManagerDelegate { func didUpdateData(_ APIManager: APIManager, data: APIManager) func didFailWithError(error: Error) } struct APIManager { let dataURL = "https://api.openweathermap.org/data/2.5/weather?appid=MyAPIID&units=imperial" var delegate: APIManagerDelegate? func fetchData(location: String) { let urlString = "\(dataURL)&q=\(location)" performRequest(urlString: urlString) /*print(urlString)*/ } func performRequest(urlString: String) { if let url = URL(string: urlString) { let session = URLSession(configuration: .default) let task = session.dataTask(with: url) { (data, response, error) in if error != nil { print(error!) return } if let safeData = data { self.parseJSON(cityData: safeData) } } task.resume() } } func parseJSON(cityData: Data) { let decoder = JSONDecoder() do { let decodedData = try decoder.decode(APIResults.self, from: cityData) /*print(decodedData.name.name)*/ print(decodedData.main.temp) print(decodedData.coord.lat) print(decodedData.coord.lon) } catch { print(error) } } }
Thanks for the replies, I had to no idea URLComponents was a thing.
I've reformatted the code to look like this:
however nothing is printing in the debug console, so I'm thinking there may still be a problem w/how the URL is being formatted. Is there something I'm missing/ not understanding?
I've reformatted the code to look like this:
Code Block import UIKit import CoreLocation protocol APIManagerDelegate { func didUpdateData(_ APIManager: APIManager, data: APIManager) func didFailWithError(error: Error) } struct APIManager { let dataURL = "https://api.openweathermap.org/data/2.5/weather?" let appId = "appid=" let myApiKey = "..." var delegate = APIManagerDelegate?.self func fetchData(location: String) { var urlComponents = URLComponents(string: dataURL) urlComponents?.queryItems = [ URLQueryItem(name: "appId", value: appId), URLQueryItem(name: "units", value: "imperial"), URLQueryItem(name: "q", value: location), ] performRequest(url: urlComponents?.url) } func performRequest(url: URL?) { if let url = url { let session = URLSession(configuration: .default) let task = session.dataTask(with: url) { (data, response, error) in if error != nil { print(error!) return } if let safeData = data { self.parseJSON(cityData: safeData) } } task.resume() } } func parseJSON(cityData: Data) { let decoder = JSONDecoder() do { let decodedData = try decoder.decode(APIResults.self, from: cityData) print(decodedData.name) print(decodedData.main.temp) print(decodedData.coord.lat) print(decodedData.coord.lon) } catch { print(error) } } }
however nothing is printing in the debug console, so I'm thinking there may still be a problem w/how the URL is being formatted. Is there something I'm missing/ not understanding?