New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alamofire 5.2 cannot convert value of type #36
Comments
@PatrickB100 since you want to map your response XML to an array directly you need to use the With that modification your code will pass compilation: AF.request(RequestUrl, method: .post, parameters: getallpersonsquery.toXML(), encoding: XMLEncoding.default, headers: headers)
.redirect(using: redirector)
.responseXMLArray { (dataResponse: DataResponse<[AllCnannelModel], AFError>) in
} |
@gcharita thanks! that made me some progress. Now i'm getting the following return: Cannot use optional chaining on non-optional value of type '[CDCatalog]' find the code below. Do you have any suggestions? thanks in advance, Patrick `class CDCatalog: XMLMappable {
|
@PatrickB100 well, the error is obvious. You are trying to access It looks like you are trying to run the README example. If that's the case then you were correct before about using AF.request(RequestUrl, method: .post, parameters: updateperson.toXML(), encoding: XMLEncoding.default)
.redirect(using: redirector)
.responseXMLObject { (response: DataResponse<CDCatalog, AFError>) in
switch response.result {
case .success(let value):
print("AF-Success")
print(value.cds?.first?.title ?? "nil")
case .failure(let error):
print("AF-Error")
print(error)
}
} Generally, which of the 2 function you have to choose depends on the XML that your API will respond to you. |
@gcharita thanks for your support! i'm indeed following the readme example. i'm getting a reponse in the debug area where it states the following: the returned body seems to be correct: can this issue be related to the fact that there is an xml version encoding utf8 header in front of the response? thanks, Patrick |
@PatrickB100 this response has nothing to do with the README example. You need to create your own model. The one that matches with your response XML. |
@PatrickB100 a possible model that you can use to map your XML is this: class Result: XMLMappable {
var nodeName: String!
var error: String?
var rowset: Rowset?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
error <- map.attributes["error"]
rowset <- map["ROWSET"]
}
}
class Rowset: XMLMappable {
var nodeName: String!
var rows: [Row]?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
rows <- map["ROW"]
}
}
class Row: XMLMappable {
var nodeName: String!
var name: String?
var prefix: String?
var firstName: String?
var homeAddress: String?
var homeZip: String?
var homeCity: String?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
name <- map["NAME"]
prefix <- map["PREFIX"]
firstName <- map["FIRSTNAME"]
homeAddress <- map["HOMEADDRESS"]
homeZip <- map["HOMEZIP"]
homeCity <- map["HOMECITY"]
}
} Calling your API with Alamofire may look like this: AF.request(RequestUrl, method: .post, parameters: updateperson.toXML(), encoding: XMLEncoding.default)
.redirect(using: redirector)
.responseXMLObject { (response: DataResponse<Result, AFError>) in
switch response.result {
case .success(let value):
print("AF-Success")
print(value.rowset?.rows?.first?.name ?? "nil")
case .failure(let error):
print("AF-Error")
print(error)
}
} |
@gcharita Thank you very much! it's working now. Do you know if there is a README on how to put reponse data from XMLMapper into an UITableView? Patrick |
@PatrickB100 you need to use |
Closing this since question have been answered and there is no issue. |
Hi GCharita,
Since alamofire 5.2 i cannot convert DataResponse to expected type (DataResponse<T, AFError>).
Please find attached the used code and a screenshot of the error. Can you help me throught with this issue?
best regards,
Patrick
Alamofire 5.2
XMLMapper 2.0.0
Xcode 12.3
The text was updated successfully, but these errors were encountered: