Jr Takhirovich
Jr Takhirovich

Reputation: 55

Alamofire Get Response

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)
            }
    }
}

here get request enter image description here

My SelectCourseResponse struct

enter image description here

Upvotes: 2

Views: 102

Answers (2)

Muhammad Afzal
Muhammad Afzal

Reputation: 201

Your price, icon and update_date fields are null declare them as optional and your code will work.

Upvotes: 3

Rob C
Rob C

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

Related Questions