ios - Swift NSURL nil when running the application -
when run application xcode told me unexpectedly found nil while unwrapping optional value @ url url isn't nil, can help?
here code
import foundation protocol weatherundergroundservicebygeographicaldelegate{ func setweatherbygeographical(weather:weatherunderground) } class weatherundergoundservicebygeographical{ var delegate:weatherundergroundservicebygeographicaldelegate? func getweatherfromweatherunderground(latitude:double, longitude:double){ let path = "http://api.wunderground.com/api/48675fd2f5485cff/conditions/geolookup/q/\(latitude,longitude).json" let url = nsurl(string: path) //session let session = nsurlsession.sharedsession() //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~error @ here~~~~~~~~~~~~~~~~~~~~~~~~~ let task = session.datataskwithurl(url!) { (data:nsdata?, response:nsurlresponse?, error:nserror?) -> void in //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ let json = json(data: data!) //parsing json weather condition weather api. using swiftyjson let name = json["current_observation"]["display_location"]["city"].string let temp = json["current_observation"]["temp_c"].double let windsp = json["current_observation"]["wind_mph"].double //prasing weather data let weather = weatherunderground(cityname: name!, temperature: temp!, windspeed: windsp!) if self.delegate != nil{ dispatch_async(dispatch_get_main_queue(), { () -> void in self.delegate?.setweatherbygeographical(weather) }) } } task.resume() } }
you have error in path string, try this:
let path = "http://api.wunderground.com/api/48675fd2f5485cff/conditions/geolookup/q/\(latitude),\(longitude).json" the reason interpolating tuple value \(latitude,longitude) in string, adds space , makes url string invalid because space not percent-escaped. instead have interpolate each value comma between them: \(latitude),\(longitude)
Comments
Post a Comment