AlamofireObjectMapper를 처음 사용하여 신속하게 json 응답을 구문 분석하려고합니다.AlamofireObjectMapper in swift 3
응답은 다음과 같습니다
"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!)
}
}
}
그것은 나에게 오류 매핑이 오류를 제공합니다 :
cannot convert value 'String' to expected argument type 'DispatchQueue?'
당신이 말한대로했는데 arrayCurency 오류의 인용문 행에 오류가 있습니다. 형식 ModelCurrency가 프로토콜 Sequence를 준수하지 않습니다. 어떻게해야합니까? –
첫 번째 rwite responsObject 내가 가지고있는 오류 : 'ModelCurrence'형식의 프로토콜을 준수하지 않습니다. 신속한 처리 하지만 응답을 작성합니다. 배열에 오류가 있습니다. ValueTupe ModelCurrence가 'name'및 'val'멤버가 아닙니다. –
Alamofire .request (URL) .responseObject {(응답 : DataResponse) in arrayCurency = response.result.value! arrayCurency에서 따옴표에 대한 { 인쇄 (! quotes.name) 인쇄 (quotes.val!)} } –