user2661518
user2661518

Reputation: 2755

try catch variable in swift

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 usereturn 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)
    }
}

Upvotes: 1

Views: 3079

Answers (3)

Jake Braden
Jake Braden

Reputation: 489

I personally would use an if let, which I believe is what you are essentially trying to do.

if let value = Double(label.text!)!{
    print("value\(value)")
}else{
    print("hi")
}

Please let me know if this fits what you are trying to do, and if not, I'll be happy to help in any other way!

UPDATE:

if let value = Double(label.text!){
        print("value\(value)")
    }else{
        print("hi")
    }

This is the correct way. Note: The label text is only unwrapped, not the whole double. If the label.text! is a valid number ("3.14159") and not text like ("hello"), then the value will be printed. If not, the else statement will catch it.

UPDATE 2:

WORKING:

Declaration:

var displayValue: Double{
    get{
        return Double(label.text!)!
    }
    set{
        displayLabel.text! = String(newValue)
    }
}

Function:

if let value = Double(label.text!){
        print("value\(value)")

        displayLabel.text! = "\(displayValue)"

    }else{
        print("hi")
    }

Upvotes: 1

J Manuel
J Manuel

Reputation: 3070

Actually, it does exist a try-catch operation in Swift. In the official documentation at Apple's Website, explains that this should be done like:

do {
    try expression
    statements
} catch pattern 1 {
    statements
} catch pattern 2 where condition {
    statements
}

For example:

var vendingMachine = VendingMachine()
vendingMachine.coinsDeposited = 8
do {
    try buyFavoriteSnack("Alice", vendingMachine: vendingMachine)
} catch VendingMachineError.InvalidSelection {
    print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
    print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let coinsNeeded) {
    print("Insufficient funds. Please insert an additional \(coinsNeeded) coins.")
}

The source of this can be found here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

Upvotes: 1

Lu_
Lu_

Reputation: 2685

making double from string does not throw so you will not catch anything, you should

if let value = Double(label.text) {
    //here it worked out 
    print("value \(value)")
} else {
    //it failed
}

Upvotes: 2

Related Questions