简体   繁体   English

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

[英]ObjectMapper and Alamofire issue for Swift 3 Alamofire version for Alamofire 4

I have recently migrated to Swift so please be patient and supportive. 我最近迁移到Swift因此请耐心和支持。 I used the code from Object Mapper github page correcting the Syntax for Alamofire 4 我使用了来自Object Mapper github页面的代码来更正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)           
        }
    }
}

But I get the following error as shown in screenshot. 但是我得到了以下错误,如屏幕截图所示。

屏幕截图

This way you should call method in Alamofire : 这样,您应该在Alamofire中调用method:

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)
            }
        }
    }

Hope this gonna help you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM