0
votes

I am having an error of

        EduappRestClient.request(with: URLString, method: .post, parameters: parameters) { (json, error) in
        guard error == nil, let json = json else {
            completion(nil, error)
            return
        }
        let result = try JSONDecoder().decode(QuestionModel.self, from: json)
        completion(result, nil)
    }

it's an API i am calling and my full source code can be found at https://github.com/WilliamLoke/quizApp

may i know what is the issue i am getting this line of error code?

2
It looks like the function expects a block that doesn't throw, but you're using try outside of a do {} catch block. Try wrapping the throwing portion of your function in a do catch, or use try?. - daltonclaybrook
@WilliamLoke Please read the Error Handling chapter in the Swift book. - rmaddy

2 Answers

1
votes

Since this block is not expected to throw an error, you need to wrap your throwing call in a do catch block:

EduappRestClient.request(with: URLString, method: .post, parameters: parameters) { (json, error) in
    guard error == nil, let json = json else {
        completion(nil, error)
        return
    }
    do {
        let result = try JSONDecoder().decode(QuestionModel.self, from: json)
        completion(result, nil)
    } catch let error {
        completion(nil, error)
    }
}
0
votes

I've got the same problem, and the solution is easy: you can just use try? instead of try

guard let result = try? JSONDecoder().decode(QuestionModel.self, from: json)