Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift try with question mark

I see the following in the code maintained by me:

func parse(values: NSMutableDicationary) {
    let data = try? JSONSerialization.data(withJSONObject: values, options: JSONSerialization.WritingOptions())
}

Pay attention that the method does not mark as throwing anything nor does it handle the error. The code does crash the application though.

I am trying to figure out what does try? (try with a question mark) means.

Googling or StackOverflowing did not return any useful info.

So, what does try? mean in Swift?

like image 295
UrK Avatar asked Sep 19 '17 11:09

UrK


People also ask

What is the difference between try and try in Swift?

Similar to try? , try! doesn't need to be put inside do/catch block as well. The difference is that when an error is thrown, your app will crash instead of returning nil. This is similar to unwrapping a nil value in optionals.

How do you do a question mark in Swift?

Write a question mark (?) after the type of a value to mark the value as optional . If the optional value is nil , the conditional is false and the code in braces is skipped.

What does the question mark mean in Swift?

You specify optional chaining by placing a question mark ( ? ) after the optional value on which you wish to call a property, method or subscript if the optional is non- nil . This is very similar to placing an exclamation point ( ! ) after an optional value to force the unwrapping of its value.

What does two question marks mean in Swift?

Double question mark is a nil-coalescing operator. In plain terms, it is just a shorthand for saying != nil . First it checks if the the return value is nil, if it is indeed nil, then the left value is presented, and if it is nil then the right value is presented.


2 Answers

If you mark a try with a question mark as try?, the return value of the throwable function will be optional. It will be nil in case the function threw an error (so Swift returns a nil instead of throwing the error, hence you don't need a do-catch block) or it will be the wrapped return value of the function in case no error was thrown.

This is how you can mimic the behaviour in your own function:

func parse(values: NSMutableDicationary)->Data?{
    do {
        return try JSONSerialization.data(withJSONObject: values, options: JSONSerialization.WritingOptions())
    } catch {
        return nil
    }
}

This is essentially the same as:

func parse(values: NSMutableDicationary)->Data?{
    return try? JSONSerialization.data(withJSONObject: values, options: JSONSerialization.WritingOptions())
}
like image 166
Dávid Pásztor Avatar answered Oct 24 '22 12:10

Dávid Pásztor


From apple documentation, try? handles an error by converting it to an optional value. If an error is thrown while evaluating the try? expression, the value of the expression is nil.

So the result is one of two:

A. there is error thrown and data is nil, and trying to use the data variable is what causing the crash

B. the thrown error is not catched by try? which happens a lot in functions that run async for some reason

like image 35
Hadi Avatar answered Oct 24 '22 12:10

Hadi