AlamofireObjectMapper in swift 3

时间:2016-11-24 18:00:48

标签: json swift3 alamofire objectmapper

我想第一次使用AlamofireObjectMapper来解析swift中的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?'

enter image description here

2 个答案:

答案 0 :(得分:1)

有几个问题。

  1. 您想引用DataResponse<ModelCurrency>而不是DataResponse<[Quotes]>

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

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

  4. quotes键关联的值不是对象数组。这是另一本字典。因此,您应该将其映射到字典,然后使用map方法将其转换为Quote个对象的数组。

  5. 所以,把所有这些拉到一起:

    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,因为它似乎是单一货币的报价。)

答案 1 :(得分:0)

问题在于这部分代码:

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

在行情中。

你应该做的是地图字典:

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

当映射使用时:

quotes  <- map["quotes"]

看看:

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

在基础知识上有映射字典。

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

没有看到您的图片 - 但如果您没有更改,则该应用会崩溃。

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

https://github.com/tristanhimmelman/AlamofireObjectMapper

寻找阵列响应