Value in null label or 0

0

How to do so if (for example in two boxes to perform an operation) in one of the text boxes you have not entered any value, do not close the application.

and for label boxes?

@IBOutlet weak var N1: UITextField!
@IBOutlet weak var N2: UITextField!
@IBOutlet weak var N3: UITextField!

@IBOutlet weak var P1: UITextField!
@IBOutlet weak var P2: UITextField!
@IBOutlet weak var P3: UITextField!

@IBOutlet weak var F1: UILabel!
@IBOutlet weak var F2: UILabel!
@IBOutlet weak var F3: UILabel!

@IBOutlet weak var res1: 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 tot(_ sender: Any) {


 let n1 = Double(N1.text!)
 let p1 = Double(P1.text!)
   let f1 = Double(n1! * p1! / 100)

    F1.text = "\(f1)"



    let n2 = Double(N2.text!)
    let p2 = Double(P2.text!)
    let f2 = Double(n2! * p2! / 100)

    F2.text = "\(f2)"


    let n3 = Double(N3.text!)
    let p3 = Double(P3.text!)
    let f3 = Double(n3! * p3! / 100)

    F3.text = "\(f3)"


    guard let a = Double(F1.text!), let b = Double(F2.text!), let c = Double(F3.text!) else
    {
        return
    }

    let suma = a + b + c
    self.res1.text = String(suma)


       }

}

    
asked by Marina Ad 26.03.2017 в 22:46
source

2 answers

1

You can also use the coalescence operator ?? . This operator checks if an optional has value, or conversely if it is nil . And in this last case, assign the value you put on your right.

so let var1 = N1.text ?? "default" will assign var1 the value of N1, and if this is nil , it would assign "default"

Like this:

@IBAction func tot(_ sender: Any) {


   let n1 = Double(N1.text ?? "") ?? 0.0
   let p1 = Double(P1.text ?? "") ?? 0.0

   let f1 = Double(n1 * p1 / 100)

   F1.text = "\(f1)"


   let n2 = Double(N2.text ?? "") ?? 0.0
   let p2 = Double(P2.text ?? "") ?? 0.0

   let f2 = Double(n2 * p2! / 100)

   F2.text = "\(f2)"


   let n3 = Double(N3.text ?? "") ?? 0.0
   let p3 = Double(P3.text ?? "") ?? 0.0

   let f3 = Double(n3 * p3 / 100)

   F3.text = "\(f3)"


   let suma = f1 + f2 + f3
   self.res1.text = String(suma)


}
    
answered by 27.03.2017 / 09:10
source
0

Hello, try this again and confirm if it works for you

class ViewController: UIViewController {

    @IBOutlet weak var F1: UILabel!
    @IBOutlet weak var F2: UILabel!
    @IBOutlet weak var F3: UILabel!
    @IBOutlet weak var rest: UILabel!

    @IBOutlet weak var N1: UITextField!
    @IBOutlet weak var N2: UITextField!
    @IBOutlet weak var N3: UITextField!


    @IBOutlet weak var P1: UITextField!
    @IBOutlet weak var P2: UITextField!
    @IBOutlet weak var P3: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
    }


    @IBAction func presionarBoton(_ sender: Any) {

        if let n1 = Double(N1.text!), let n2 = Double(N2.text!), let n3 = Double(N3.text!), let p1 =  Double(P1.text!), let p2 = Double(P2.text!), let p3 = Double(P3.text!){

            let f1 = Double(n1 * p1 / 100)
            F1.text = String(f1)

            let f2 = Double(n2 * p2 / 100)
            F2.text = String(f2)

            let f3 = Double(n3 * p3 / 100)
            F3.text = String(f3)


        }else {
            print("Llene todos los campos")
            self.F1.text = ""
            self.F2.text = ""
            self.F3.text = ""
            self.rest.text = ""
        }

        guard let a = Double(F1.text!), let b = Double(F2.text!), let c = Double(F3.text!) else {
            return
        }

        let suma = a + b + c
        self.rest.text = String(suma)

    }

}

I recommend you see these basic courses

link

And also read this documentation link

    
answered by 27.03.2017 в 05:43