针对Alamofire 4的Swift 3 Alamofire版本的ObjectMapper和Alamofire问题

时间:2017-01-15 18:56:58

标签: ios swift3 alamofire objectmapper

我最近迁移到Swift所以请耐心和支持。 我使用Object Mapper github page中的代码更正了Alamofire 4

的语法
let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
Alamofire.request(URL).response { (response: DataResponse<WeatherResponse>) in

    let weatherResponse = response.result.value
    print(weatherResponse?.location)

    if let threeDayForecast = weatherResponse?.threeDayForecast {
        for forecast in threeDayForecast {
            print(forecast.day)
            print(forecast.temperature)           
        }
    }
}

但是我收到了如屏幕截图所示的以下错误。

ScreenShot

1 个答案:

答案 0 :(得分:0)

这样你应该在Alamofire中调用方法:

func getWeatherDataResponseFromServer() {

        let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"

        //AlamoFire request
        Alamofire.request(URL, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
            do {

                let reponse = try JSONSerialization.jsonObject(with: response.data!, options: JSONSerialization.ReadingOptions()) as! Dictionary<String, AnyObject>

            } catch {
                print(error)
            }
        }
    }

希望这会帮助你。