Reputation: 55
I get a nil in let restResponse = try? JSONDecoder().decode(SelectCourseResponse.self, from: data)
any reasons ?
func getMotherLangRequest() {
showActivityIndicatory(uiView: view, show: true)
AF.request(NetworkUrl.motherLang_getRequest,
method: .get)
.responseJSON { response in
self.showActivityIndicatory(uiView: self.view, show: false)
debugPrint(response)
switch response.result {
case .success:
guard let data = response.data else { return }
let restResponse = try? JSONDecoder().decode(SelectCourseResponse.self, from: data)
if restResponse?.status == 0 {
} else { self.showErrorResponse(data: data) }
case let .failure(error): print(error)
}
}
}
My SelectCourseResponse struct
Upvotes: 2
Views: 102
Reputation: 201
Your price, icon and update_date fields are null declare them as optional and your code will work.
Upvotes: 3
Reputation: 5073
I would recommend using a do/catch block and printing out the error. That said, you have a number of null fields in your JSON object but you didn't define those fields as optional in your data structures. update_date
, price
, and icon
should all be optionals.
do {
let restResponse = try JSONDecoder().decode(
SelectCourseResponse.self,
from: data
)
process(restReponse)
} catch {
print("DECODE ERROR: \(error)")
}
Upvotes: 2