I am trying to add try catch against variable. Without try catch I this error:
fatal error: unexpectedly found nil while unwrapping an Optional value for variable Double(label.text!)!
So I want to catch above error. I tried below
do{
let value = try Double(label.text!)!
print("value\(value)")
} catch{
print("hi")
}
But it still gives same error and I also see this warnings:
No calls to throwing functions occur within try and catch block in unreachable...
Is this right way of using try catch block in swift?
edit: (not duplicate) If I just return return Double(labelDisplay.text)
I get compilation error value of option type String? not unwrapped, so I have to use
return Double(labelDisplay.text!)!` which is where if fails. That's why I was trying to catch it.
another edit: label is @IBOutlet weak private var label: UILabel!
edit: return code
var displayValue: Double{
get{
print(labelDisplay.text.dynamicType)
return Double(labelDisplay.text!)!
}
set{
labelDisplay.text! = String(newValue)
}
}