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

3

13

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.

I have validated that my source JSON is good. I've been searching and cannot find a solution to this problem

import Foundation


class lenderDetails
{

func loadLender()
{

    let lenders = ""

    let url = URL(string: lenders)!
    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)
    }

    session.resume()
}
}

Thank you for all the help!

Rooney answered 31/5, 2017 at 23:52 Comment(0)
C
4

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
}
Contemplation answered 8/6, 2017 at 16:51 Comment(0)
B
59

The SwiftyJSON initializer throws, the declaration is

public init(data: Data, options opt: JSONSerialization.ReadingOptions = []) throws

You have three options:

  1. Use a do - catch block and handle the error (the recommended one).

    do {
       let json = try JSON(data: data)
       print(json)
    } catch {
       print(error)
       // or display a dialog
    }
    
  2. Ignore the error and optional bind the result (useful if the error does not matter).

    if let json = try? JSON(data: data) {
       print(json)
    }
    
  3. Force unwrap the result

    let json = try! JSON(data: data)
    print(json)
    

    Use this option only if it's guaranteed that the attempt will never fail (not in this case!). Try! can be used for example in FileManager if a directory is one of the default directories the framework creates anyway.

For more information please read Swift Language Guide - Error Handling

Binette answered 8/6, 2017 at 17:4 Comment(5)
This sould be the selected answer!Mattos
Thanks no 3 work for me.. don't know why but no 1 and 2 didnt work =-=" any idea why? *just curious whyGuarino
@Guarino All three ways are supposed to work. What error message do you get?Binette
"Call can throw, but it is marked with 'try' and the error is not handled". got eror... even after trying to rebuild the project and closing the xcode workspace,,Guarino
@Guarino Check the balance of the braces of the do - catch blockBinette
C
4

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
}
Contemplation answered 8/6, 2017 at 16:51 Comment(0)
H
0

Probably you need to implement do{} catch{} block. Inside do block you have to call throwable function with try.

Howdoyoudo answered 1/6, 2017 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.