-4

Use try? to handling the error in the function But it shows that "No calls to throwing functions occur within 'try' expression"

     if let result:Double = try? finalformular.expression.expressionValue(with: nil, context: nil) as! Double{
        text = String(result)
        }else{
            text = "Error"
        }

enter image description here

Nick Shern
  • 23
  • 5
  • 1
    `expressionValue(with:, context:)` really throws? What's its declaration? Seems to be https://developer.apple.com/documentation/foundation/nsexpression/1410363-expressionvalue there is no "throws", so no reason to do a try. – Larme Mar 23 '19 at 07:38
  • 1
    Possible duplicate of [try, try! & try? what’s the difference, and when to use each?](https://stackoverflow.com/questions/32390611/try-try-try-what-s-the-difference-and-when-to-use-each) – El Tomato Mar 23 '19 at 07:41

1 Answers1

0

As error explains, there isn't any method which would throw. If you're using optional binding, you just need to use as? Double since you don't know if return type is Double and you need to downcast it

if let result = finalformular.expression.expressionValue(with: nil, context: nil) as? Double {
    text = String(result)
} else {
    text = "Error"
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40