作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 swift 编程语言的新手,我必须在 Swift 3 中使用 Alamofire 4.0 映射对象并遵循相同的链接> https://github.com/tristanhimmelman/AlamofireObjectMapper
但是当我复制粘贴下面的代码时,我得到了 nil
响应,有人可以帮助我了解如何使用 GET 和 POST< 映射模型对象吗/strong> 方法?
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"]
}
}
最佳答案
您的WeatherResponse
模型缺少data
节点
使用这个模型
class WeatherResponse: Mappable {
var data: WeatherResponseData?
required init?(map: Map){ }
func mapping(map: Map) {
data <- map["data"] // data
}
}
class WeatherResponseData: Mappable {
var location: String?
var threeDayForecast: [Forecast]?
required init?(map: Map){
}
func mapping(map: Map) {
location <- map["location"]
threeDayForecast <- map["three_day_forecast"]
}
}
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"]
}
}
然后打印结果
print("response is========>\(weatherResponse?.data?.location))")
关于swift - 如何在 swift 3 中使用 Alamofire->4.0 映射对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45049144/
我是一名优秀的程序员,十分优秀!