0

A big n00b here :-)

I made my first app for IOS and got everything working as long as all text fields are filled. This app is a simple calculation app and I need some help making sure that the app does not close down when fields are empty.

This is my code when a simple Calculation button is pressed.

class ViewController: UIViewController {

@IBOutlet weak var panelWidthTextField: UITextField!
@IBOutlet weak var panelHightTextField: UITextField!
@IBOutlet weak var panelsWideTextField: UITextField!
@IBOutlet weak var panelsHightTextField: UITextField!
@IBOutlet weak var panelPitchTextField: UITextField!

@IBOutlet weak var resultWithLabel: UILabel!
@IBOutlet weak var resultHightLabel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func calculatePressButton(sender: AnyObject) {
    let w = Double(panelWidthTextField.text!)
    let sw = Double(panelsWideTextField.text!)
    let pi = Double(panelPitchTextField.text!)
    let sizew = SizeWidthModel(pw:w!,psw:sw!,ptc:pi!)
    resultWithLabel.text=String(sizew.width())

    let h = Double(panelHightTextField.text!)
    let sh = Double(panelsHightTextField.text!)
    let sizeh = SizeHightModel(ph:h!,psh:sh!,ptc:pi!)
    resultHightLabel.text=String(sizeh.hight())

}

}

This is my problem. Error code

Hope someone can point me in the right direction.

Edit: As I am a complete n00b at this coding I need som more help understanding this. I have tried adding various solution with no luck. I am trying to get a message to pop up when user leave fields empty, because the calculation need all fields filled in. If someone have time to guide me that would be appreciated :-)

lightmaster2006
  • 89
  • 1
  • 10
  • `let w = Double(panelWidthTextField.text ?? "")` – vacawama Dec 20 '15 at 16:05
  • 2
    Let me point you to the right direction, then: [Swift Optionals](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID330). It's a **key** concept for a Swift programmer. :) – Eric Aya Dec 20 '15 at 16:06
  • I think you've just learned why `!` is known informally as the "crash operator" :) – TwoStraws Dec 20 '15 at 16:06
  • Possible duplicate of [What does an exclamation mark mean in the Swift language?](http://stackoverflow.com/questions/24018327/what-does-an-exclamation-mark-mean-in-the-swift-language) – Eric Aya Dec 20 '15 at 16:07

2 Answers2

0

Simply check to see if the user has entered text. In the function, put something like this

if myTextField.text != "" {
     //rest of the code
}

Hope this helps.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
0

Took me a while to figure out optionals, too, but hopefully this explanation helps: When converting from a String (which is what panelWidthTextField.text is) to a Double, Swift doesn't know if the String is valid or not. Since it might be valid or it might not contain a valid value (is nil), then it's an optional, String?, which can contain a valid String or which can be nil. But just because it can be nil doesn't mean that you can use nil in your code.

The "!" says "use the data in this variable, even if it's nil, because I don't think it's nil." Problem is, if it IS nil, then Double(nil) would crash your app.

If let (there is also an "if var") will say "look into (unwrap) this optional value. If it works, do what's in the first set of braces (which is a let or var assignment), otherwise do what's in the second set of braces.

    if let w = Double(panelWidthTextField.text!) {
        // you're good to go
    } else {
        // give the user instructions on how to fix the error.
    }

The nil coalescing operator may also help. It reads like this:

// Unwrap a and look at it. If it a isn't nil, then use it, if it is nil, then use default value b

a ?? b

If you wanted to put in a default value (like zero if you need a valid numeral), try:

var w = Double(panelWidthTextField.text!)?? 0
Gallaugher
  • 1,593
  • 16
  • 27