【发布时间】:2017-12-16 09:04:02
【问题描述】:
我是 Swift 编程语言的新手,我必须在 Swift 3 中使用 Alamofire 4.0 映射对象,并遵循相同的链接>https://github.com/tristanhimmelman/AlamofireObjectMapper
但是当我复制粘贴下面的代码时,我得到了nil 响应,有人可以帮助我了解如何使用 GET 和 POST 方法映射模型对象吗?
代码:-
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/2ee8f34d21e8febfdefb2b3a403f18a43818d70a/sample_keypath_json"
Alamofire.request(URL).responseObject { (response: DataResponse<WeatherResponse>) in
switch(response.result) {
case .success(_):
if response.result.value != nil{
let weatherResponse = response.result.value
print("response is========>\(weatherResponse?.location))")
if let threecatForday = weatherResponse?.threeDayForecast{
for forCast in threecatForday{
print("Day is======>\(forCast.day)")
print("Tempurature======>\(forCast.temperature)")
}
}
}
break
case .failure(_):
print(response.result.error!)
break
}
}
}
}
天气响应:-
import UIKit
import ObjectMapper
class WeatherResponse: Mappable {
var location: String?
var threeDayForecast: [Forecast]?
required init?(map: Map){
}
func mapping(map: Map) {
location <- map["location"]
threeDayForecast <- map["three_day_forecast"]
}
}
预测:-
import UIKit
import ObjectMapper
class Forecast: Mappable {
var day: String?
var temperature: Int?
var conditions: String?
required init?(map: Map) {
}
func mapping(map: Map) {
day <- map["day"]
temperature <- map["temperature"]
conditions <- map["conditions"]
}
}
【问题讨论】:
-
对于 GET 你可以使用这样的东西: Alamofire.request(URL, method: .get, parameters: nil, encoding: URLEncoding.default).responseObject { (response: DataResponse
) in对于 POST:Alamofire.request(URL, method: .post, parameters: nil, encoding: URLEncoding.default).responseObject { (response: DataResponse ) in... -
好的,谢谢您的回复
-
你能回答这个问题吗
标签: swift alamofire objectmapper