2017-01-15 83 views
0

我最近已迁移到Swift,所以请耐心和支持。 我使用Object Mappergithub page纠正语法的代码为Alamofire 4ObjectMapper和Alamofire问题的斯威夫特Alamofire版本为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

回答

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

希望这会帮助你。

+0

感谢您帮助我获得正确的格式,但我需要完成响应建模请参考此链接https://github.com/tristanhimmelman/AlamofireObjectMapper –

+0

然后您应该创建自己的完成处理程序方法,它返回数据你的格式,我猜 –

+0

返回的是自动写入模型类 –