【问题标题】:AlamofireObjectMapper in swift 3swift 3中的AlamofireObjectMapper
【发布时间】:2017-04-09 02:41:49
【问题描述】:

我想第一次使用 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"]
    }
}

在我的控制器中:

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

        }

    }

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

无法将值“String”转换为预期的参数类型“DispatchQueue?”

【问题讨论】:

    标签: json swift3 alamofire objectmapper


    【解决方案1】:

    有几个问题。

    1. 您想引用 DataResponse&lt;ModelCurrency&gt; 而不是 DataResponse&lt;[Quotes]&gt;

    2. 您想使用responseObject,而不是responseArray

    3. 您不想将 queue 参数与 String 值一起使用。 queue 参数用于指定您希望完成处理程序运行哪个调度队列。但这不是你在这里做的,所以你应该删除它。

    4. quotes 键关联的值不是对象数组。它是另一本字典。所以你应该将它映射到一个字典,然后使用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,因为它似乎是单一货币的报价。)

    【讨论】:

    • 我按照你说的做了,但是arrayCurrency中的引号连续出现错误错误:ModelCurrency类型不符合协议序列。我该怎么办?
    • 第一个我 rwite responsObject 我有错误:类型'ModelCurrence'不符合协议序列 swift 但我写响应数组我有错误:值 tupe ModelCurrence has not member a 'name' and 'val'
    • Alamofire.request(URL).responseObject { (response: DataResponse) in let arrayCurency = response.result.value!对于 arrayCurency { print(quotes.name!) print(quotes.val!) } } 中的引号
    • Alamofire.request(URL).responseObject { (response: DataResponse&lt;ModelCurrency&gt;) in let currency = response.result.value!; for quotes in currency.quotes { print(quotes.name!); print(quotes.val!) } }
    • 非常感谢您的帮助)我首先写信给 Swift 一切正常
    【解决方案2】:

    问题在于这部分代码:

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

    在引号中。

    你应该做的是地图字典:

    var quotes: [String : CGFloat] = [:]
    

    当映射使用时:

    quotes  <- map["quotes"]
    

    看看:

    https://github.com/Hearst-DD/ObjectMapper

    在基础上有映射字典。

    更具体地说,在引用对象中,您没有 name 和 val JSON 对象,这正是您想要的。如果您将其映射到字典,您将能够访问这些值。

    还没有看到你的图片 - 但如果你不改变上面的应用程序将会崩溃。

    对于上述问题,您需要提供要在其中运行请求的队列,如果您不想弄乱它并且实际上想要 keyPath,那么您需要使用 keyPath: 而不是 @ 调用函数987654327@。链接:

    https://github.com/tristanhimmelman/AlamofireObjectMapper

    查找数组响应

    【讨论】:

    • 我的问题是 .responseArray(queue: "quotes") 错误无法将值 'String' 转换为预期的参数类型 'DispatchQueue?'我写了 var 引号: [String : CGFloat] = [:] 它什么也没改变。 var 成功:布尔? var 条款:字符串? var 隐私:字符串? var 时间戳:CGFloat?变量来源:字符串? var 引号:[String : CGFloat] = [:]
    • 看看其余的答案 - 你有没有试过而不是队列来提供 keyPath ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2017-09-23
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多