1

I have problem with this for so long. I'm thinking about adding this question so long, but i decided to do this. So:

How to get response in getWeather func? Its my first app in swift. I'm a c# developer, swift is a magic for me. Thx for your help. :)

class ApiConnector{

    var validator = Validator();
    var  weatherMapper = WeatherMapper();


    public func ApiConnector(){}

    public func getWeather(city: String) -> Weather{
        let urlString = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&lang=pl&units=metric&appid="+Configuration.AppTokenId;

        if validator.Validate(city: city) {
            let response = request(url: urlString)

            return weatherMapper.Map(Json: response);
        }
        return Weather();

    }


    private func request(url: String) -> [String: Any]{

        let config = URLSessionConfiguration.default // Session Configuration
        let session = URLSession(configuration: config) // Load configuration into Session
        let url = URL(string: url)!
        var tmp = [String: Any]();

        let task = session.dataTask(with: url, completionHandler: {
            (data, response, error) in

            if error != nil {

                print(error!.localizedDescription)

            } else {

                do {

                    if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
                    {

                        tmp = json;

                        print(tmp);
                    }

                } catch {

                    print("error in JSONSerialization")

                }
            }
        })
        task.resume()

        return tmp;

    }

    }
Dixit Akabari
  • 2,419
  • 13
  • 26

0 Answers0