gpt4 book ai didi

json - swift 3 中的 AlamofireObjectMapper

转载 作者:行者123 更新时间:2023-12-02 03:09:09 24 4
gpt4 key购买 nike

我想第一次使用 AlamofireObjectMapper 来快速解析 json 响应。

回应是:

  "success": true,
"terms": "https:\/\/currencylayer.com\/terms",
"privacy": "https:\/\/currencylayer.com\/privacy",
"timestamp": 1480007048,
"source": "USD",
"quotes": {
"USDAED": 3.672598,
"USDAFN": 66.599998,
"USDALL": 127.999937,
"USDAMD": 478.679993,
"USDANG": 1.780277,
"USDAOA": 165.072998,
"USDARS": 15.497261,
"USDAUD": 1.348899,
"USDAWG": 1.79,
"USDAZN": 1.714104,
"USDBAM": 1.855297,
"USDBBD": 2,
"USDBDT": 79.179735,
"USDBGN": 1.854199,
"USDBHD": 0.377036,
"USDBIF": 1668.300049,
"USDBMD": 1,
"USDBND": 1.429902,
"USDBOB": 6.870014,
"USDBRL": 3.396898,
"USDBSD": 1,
}

我是这样映射的:
class ModelCurrency:  Mappable {

var success : Bool?
var terms : String?
var privacy : String?
var timestamp : CGFloat?
var source : String?
var quotes : [Quotes]?

init() {}

required init?(map: Map) {

}

func mapping(map: Map) {

success<-map["success"]
terms<-map["terms"]
privacy<-map["privacy"]
timestamp<-map["timestamp"]
source<-map["source"]
quotes<-map["quotes"]

print("It json\(terms)")
}
}

class Quotes : Mappable {

var name : String?
var val : CGFloat?

required init?(map: Map) {

}

func mapping(map: Map) {
name<-map["name"]
val<-map["val"]
}
}

在我的 Controller 中:
 override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self

super.viewDidLoad()
let URL = "http://www.apilayer.net/api/live?access_key=ad847a0a855c0647590df2b818923025"

Alamofire.request(URL).responseArray(queue: "quotes") { (response: DataResponse<[Quotes]>) in
let arrayCurency = response.result.value!


for quotes in arrayCurency {
print(quotes.name!)
print(quotes.val!)
}

}

}

它给了我错误映射和这个错误:

cannot convert value 'String' to expected argument type 'DispatchQueue?'



enter image description here

最佳答案

有几个问题。

  • 您要引用DataResponse<ModelCurrency>而不是 DataResponse<[Quotes]> .
  • 您要使用 responseObject ,不是 responseArray .
  • 你不想使用那个 queue带有 String 的参数值(value)。 queue参数用于指定您希望完成处理程序运行的调度队列。但这不是你在这里做的,所以你应该删除它。
  • quotes 关联的值key 不是对象数组。它又是一本字典。所以你应该把它映射到字典,然后使用 map将其转换为 Quote 数组的方法对象。

  • 所以,把所有这些放在一起:
    Alamofire.request(urlString).responseObject { (response: DataResponse<ModelCurrency>) in
    ...
    }


    class ModelCurrency:  Mappable {

    var success : Bool?
    var terms : String?
    var privacy : String?
    var timestamp : CGFloat?
    var source : String?
    var quotes : [Quote]?

    required init?(map: Map) { }

    func mapping(map: Map) {

    success <- map["success"]
    terms <- map["terms"]
    privacy <- map["privacy"]
    timestamp <- map["timestamp"]
    source <- map["source"]

    var dictionary: [String: CGFloat]?
    dictionary <- map["quotes"]
    quotes = dictionary?.map { return Quote(name: $0.key, val: $0.value) }
    }
    }

    class Quote {

    var name : String?
    var val : CGFloat?

    init(name: String?, val: CGFloat?) {
    self.name = name
    self.val = val
    }

    }

    (我已将 Quotes 重命名为 Quote,因为它似乎是单一货币的报价。)

    关于json - swift 3 中的 AlamofireObjectMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792412/

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