martinko2499 martinko2499 - 1 month ago 26
iOS Question

Why Does Swift print "Optional(455 bytes)"

I am working on a weather app, and trying to get the data from an API. But when I type in a city name and hit enter, Swift prints out a message "Optional(455 bytes)" Not sure what goes wrong.

import Foundation

protocol WeatherServiceDelegate{
func setWeather(weather:Weather)
}

class WeatherService{
var delegate: WeatherServiceDelegate?

func getWeather(city: String){
let path = "http://api.openweathermap.org/data/2.5/weather?q=Boston"
let url = URL(string: path)

let task = URLSession.shared.dataTask(with: url!) { (data:Data?, response: URLResponse?, error: Error?) in
print(data)
}

task.resume()

Answer

There's nothing wrong, what you are trying to do is, print an iOS encoded Data from a optional variable data of type Data?

You should rather be checking the response after decoding it.

Cheers!