First of all I'm really beginner in Swift. I'm stuck for hours on this.
Here is the problem, I'm using AlamoFire in order to make my request
func getListFiles(userId: String, page: Int)
{
let parameters: Parameters = [
"userId": userId,
"page": page
]
Alamofire.request(baseUrl + Endpoints.GET_RECORDS.rawValue, method: HTTPMethod.post, parameters: parameters)
.responseData { response in
if let result = response.result.value{
do {
let data = try JSONDecoder().decode(ListFilesStruct.self, from: result)
} catch {
print("\(error)")
}
}
}
}
and I want to store data response in my structure "ListFilesStruct". But it enters in the "catch" and fails
Printing description of error:
▿ DecodingError
▿ typeMismatch : 2 elements
- .0 : Swift.Dictionary<Swift.String, Any>
▿ .1 : Context
▿ codingPath : 3 elements
- 0 : CodingKeys(stringValue: "data", intValue: nil)
▿ 1 : _JSONKey(stringValue: "Index 6", intValue: 6)
- stringValue : "Index 6"
▿ intValue : Optional<Int>
- some : 6
- 2 : CodingKeys(stringValue: "expert", intValue: nil)
- debugDescription : "Expected to decode Dictionary<String, Any> but found a string/data instead."
- underlyingError : nil
Here is the main structure, I want an array of FileDetail
struct ListFilesStruct: Codable
{
var success: Bool?
var data: [FileDetail]?
}
struct FileDetail: Codable
{
var id: String?
var expert: FileElement?
}
And it exactly fails because of FileElement structure , I don't know why
struct FileElement: Codable
{
var id: String?
var role_id: String?
var avatar: String?
var nom: String?
var prenom: String?
}
I really want to store Webservice data like that , thanks
EDIT : Expected JSON :
{
"success": true,
"data": [
{
"id": "A19007994",
"expert": {
"id": "74EJEEZM",
"role_id": "EXPERT",
"avatar": null,
"nom": "METRTALZ",
"prenom": "JEREMIE",
}
}
]
}