0

My code is crashing when i am unwrapping Double(display.text!)! i put an if condition but it didn't work

@IBAction private func buttonpress(sender: UIButton)
{
   let digit = sender.currentTitle!
    if userisinthemiddle
    {
        let currenttext = display.text!
        display.text = currenttext + digit
    }
    else
    {
        display.text = digit
    }
    userisinthemiddle = true
}

till here it is working properly but when i try to make it as a property it is not working

var DisplayValue : Double
{
    get
    {
         return Double(display.text!)!  // thread 1 
      }

    set
    {
        display.text = String(newValue)
    }
}
  • what happens if the `display.text` is `"abcdef"`? – luk2302 Jun 19 '16 at 12:35
  • Either `text` is `nil` or the string cannot be represented by a `Double`. Check that. – vadian Jun 19 '16 at 12:36
  • You might want to use the *nil coalescing operator* `??` instead of force unwrap `!` to avoid crashes: `Double(display.text ?? "") ?? 0`. – vacawama Jun 19 '16 at 12:40
  • Can you add information on what specifically is not working? Where is your code crashing? Have you added print statements to inspect the values of your variables? – Alfie Hanssen Jun 19 '16 at 12:48
  • 1
    Please consult the extensive answer on the linked page. It will help you understand what is happening and how to fix it. – Eric Aya Jun 19 '16 at 13:27

1 Answers1

1

It's generally not a good idea to force unwrap a variable unless you actually want your program to crash in the event of a failure (in my experience this is rare). In this case it sounds like it's inhibiting your ability to diagnose the problem. Try something like this to (1) avoid force unwrapping and (2) be in a better position to react to unexpected values.

@IBAction private func buttonPress(sender: UIButton)
{
    guard let digit = sender.currentTitle else 
    {
        assertionFailure("digit is nil.")

        return
    }

    print("digit: \(digit)"

    if userIsInTheMiddle
    {
        let currentText = display.text ?? "" // If display.text is nil, set currentText to an empty string
        print("currentText: \(currentText)"

        display.text = currentText + digit
    }
    else
    {
        display.text = digit
    }

    print("display.text: \(display.text)"

    userIsInTheMiddle = true
}

var displayValue: Double
{
    get
    {
         let text = display.text ?? ""

         guard let double = Double(text) else 
         {
             // You probably want to replace this `assertionFailure` and return a default value like 0

             assertionFailure("text could not be converted to a Double")

             return
         }

         return double
    }

    set
    {
        display.text = String(newValue)
    }
}

A few questions:

  1. How does the property displayValue relate to the @IBAction?
  2. You're using the + operator to concatenate two strings here display.text = currentText + digit. Just confirming you're not trying to add two numbers?
Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74
  • thanks alot for the answer and explanation 1. i made displayValue property once so that i dont have to convert string to double and double to string again and again 2. yeah i am trying to concatenate if currenttext contains "2" and digit contains "3" then i want to print "23" – Khizar Khalifa Jun 20 '16 at 12:42