1

Can anyone help me with this API code. I got everything but one error fixed.

Here is my code:

 let APIUrl = NSURL(string:"https://api.openweathermap.org/data/2.5/weather?   lat=35&lon=150&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric")
 var request = URLRequest(url:APIUrl! as URL)

 let task = URLSession.shared.dataTask(with: request as URLRequest)

 guard let data = Data else {return}

 let decoder = JSONDecoder()

 let weatherData = try decoder.decode(MyWeather, from: data)

 let ggtemp = weatherData.main?.temp
    print(ggtemp, "THIS IS THE TEMP")
        DispatchQueue.main.async {
        tempDisplay.text = String (ggtemp) + " c"
     }
 }

Image of error

Once I fix the "let data = data" error, I get an error on the "let task = URLSesss..."

Any help would be appreciated. Thanks in advance.

B. Deaton
  • 55
  • 1
  • 12
  • What is the error you are getting? – Mike Taverne Oct 21 '18 at 05:32
  • I fixed the "let data = Data() and now I am getting an error in the "let weatherData = try decoder.decode(MyWeather, from: Data) error is: Cannot convert value of type 'Data.Type' to expected argument type 'Data' – B. Deaton Oct 21 '18 at 05:37
  • 1
    You have to run the task, which will perform an asynchronous call and download the data. Perhaps you should start with a [good tutorial](https://www.raywenderlich.com/567-urlsession-tutorial-getting-started) instead – MadProgrammer Oct 21 '18 at 05:40

1 Answers1

0

Try this code

let APIUrl = NSURL(string:"https://api.openweathermap.org/data/2.5/weather?lat=35&lon=150&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric")
var request = URLRequest(url:APIUrl! as URL)
request.httpMethod = "GET"

let dataTask = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in

    if (error != nil) {
        print(error ?? "Error is empty.")
    } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse ?? "HTTP response is empty.")
    }

    guard let responseData = data else {
        print("Error: did not receive data")
        return
    }

    do {

        let weatherData = try JSONDecoder().decode(MyWeather.self, from: responseData)
        let ggtemp = weatherData.main?.temp
        print(ggtemp, "THIS IS THE TEMP")
        DispatchQueue.main.async {
            tempDisplay.text = String (ggtemp) + " c"
        }

    } catch  {
        print("error parsing response from POST on /todos")
        return
    }

})

dataTask.resume()
Sabbir Ahmed
  • 351
  • 1
  • 13
  • This seems to be working but I think the API is messed up. I am trying to get the lat/long of my current location but it's not working with lat and long variables to add to the api. 'var lat = location.coordinate.latitude var long = location.coordinate.longitude let APIUrl = NSURL(string:"https://api.openweathermap.org/data/2.5/weather?lat="lat"&lon="long"&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric")' – B. Deaton Oct 21 '18 at 06:12
  • 1
    first you need to get the current longitude and latitude of a location then set your lat & long value how to get the current longitude and latitude of a location see this url https://stackoverflow.com/questions/26741591/how-to-get-current-longitude-and-latitude-using-cllocationmanager-swift after then set lat & long value like this -> let APIUrl = NSURL(string:"https://api.openweathermap.org/data/2.5/weather?lat=\(currentLocation.coordinate.latitude))&lon=\(currentLocation.coordinate.longitude)&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric") – Sabbir Ahmed Oct 21 '18 at 06:47
  • Awesome. Thanks Sabbir. Now I'm getting error parsing response from POST on /todos. I'll keep trying. – B. Deaton Oct 21 '18 at 09:53
  • 1
    Welcome B.Deaton, if any help knock me this my Skype id sabbir.cn – Sabbir Ahmed Oct 21 '18 at 10:17