swiftyjson - Call can throw, but it is marked with 'try' and the error is not handled

前端 未结 3 434
情话喂你
情话喂你 2020-12-11 16:20

I am trying to use swiftyjson and I am getting an Error:

Call can throw, but it is marked with \'try\' and the error is not handled.

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 16:31

    You should wrap it into a do-catch block. In your case:

    do {
        let session =  URLSession.shared.dataTask(with: url) {
            (data, response, error) in
                guard let data = data else {
                print ("data was nil?")
                return
            }
    
            let json = JSON(data: data)
            print(json)
        }
    } catch let error as NSError {
        // error
    }
    

提交回复
热议问题