gpt4 book ai didi

swift - 如何在 swift 3 中使用 Alamofire->4.0 映射对象

转载 作者:行者123 更新时间:2023-11-28 08:06:11 25 4
gpt4 key购买 nike

我是 swift 编程语言的新手,我必须在 Swift 3 中使用 Alamofire 4.0 映射对象并遵循相同的链接> https://github.com/tristanhimmelman/AlamofireObjectMapper

但是当我复制粘贴下面的代码时,我得到了 nil 响应,有人可以帮助我了解如何使用 GETPOST< 映射模型对象吗/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
}
}
}
}

WetherResponse:-

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 节点

enter image description here

使用这个模型

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com