5

I am using an SQLite library in which queries return optional values as well as can throw errors. I would like to conditionally unwrap the value, or receive nil if it returns an error. I'm not totally sure how to word this, this code will explain, this is what it looks like:

func getSomething() throws -> Value? {
    //example function from library, returns optional or throws errors
}


func myFunctionToGetSpecificDate() -> Date? {
    if let specificValue = db!.getSomething() {
         let returnedValue = specificValue!
         // it says I need to force unwrap specificValue, 
         // shouldn't it be unwrapped already?

         let specificDate = Date.init(timeIntervalSinceReferenceDate: TimeInterval(returnedValue))
         return time
    } else {
         return nil
    }

}

Is there a way to avoid having to force unwrap there? Prior to updating to Swift3, I wasn't forced to force unwrap here.

The following is the actual code. Just trying to get the latest timestamp from all entries:

func getLastDateWithData() -> Date? {
    if let max = try? db!.scalar(eventTable.select(timestamp.max)){

        let time = Date.init(timeIntervalSinceReferenceDate: TimeInterval(max!))

        // will max ever be nil here? I don't want to force unwrap!
        return time

    } else {
        return nil
    }
}
twiz_
  • 1,178
  • 10
  • 16

2 Answers2

9

Update: As of Swift 5, try? applied to an optional expression does not add another level of optionality, so that a “simple” optional binding is sufficient. It succeeds if the function did not throw an error and did not return nil. val is then bound to the unwrapped result:

if let val = try? getSomething() {
    // ...
}

(Previous answer for Swift ≤ 4:) If a function throws and returns an optional

func getSomething() throws -> Value? { ... }

then try? getSomething() returns a "double optional" of the type Value?? and you have to unwrap twice:

if let optval = try? getSomething(), let val = optval {

}

Here the first binding let optval = ... succeeds if the function did not throw, and the second binding let val = optval succeeds if the return value is not nil.

This can be shortened with case let pattern matching to

if case let val?? = try? getSomething() {

}

where val?? is a shortcut for .some(.some(val)).

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Exactly what I was looking for. I haven't seen if case let before, I will have to read about it. Also what do you mean by .some(.some(val)). – twiz_ Sep 25 '16 at 20:24
  • @twiz_: Optional is an enumeration with cases `.none` and `.some(wrapped)`. `.some(.some(val))` is a nested optional value, which is not nil, and the unwrapped value is also not nil. Here is another example of using nested optionals http://stackoverflow.com/questions/27225232/two-or-more-optionals-in-swift. – Martin R Sep 25 '16 at 20:34
  • Tricky, because if you are only interested in the success of the call, and not the returned data, you may easily be confused if the call returns nil to indicate failure. – Mike Taverne Mar 06 '18 at 03:47
  • Thanks for the answer. What if it returns nothing? Can I use `let optval = try? doSomething()` and assume it will be nil if `doSomething` throws an error, and non-nil if it didn't? – agirault Mar 22 '19 at 19:09
0

I like Martin's answer but wanted to show another option:

if let value = (try? getSomething()) ?? nil {

}

This has the advantage of working outside of if, guard, or switch statements. The type specifier Any? isn't necessary but just included to show that it returns an optional:

let value: Any? = (try? getSomething()) ?? nil
Jesse
  • 1,667
  • 12
  • 16