0

I am trying to have a scene in my app that allows the user to enter numbers in two separate Text Fields, which does some basic math, and spits out the output into a Label when the click the "Calculate button". I have watched videos, changed code, and I can't seem to get it to work the way I want to, and my app keeps crashing. Here is the simple code I have so far. Can anyone explain what I'm entering in that is wrong and what I need to change? It will be much appreciated, Thanks!

import UIKit

class DopamineCalculator: UIViewController {

//Patient weight Input

@IBOutlet weak var patientWeight: UITextField!


//calculate button
@IBAction func calculateButton(sender: AnyObject) {

    let weightPatient = Double(self.patientWeight.text!)

    dripAnswer.text = String(weightPatient * 2)

}


//Clear button action
@IBAction func clearButton(sender: AnyObject) {
    self.patientWeight.text=nil
    //self.dosageDesired.text=nil
    self.dripAnswer.text=nil
}


//Drip Rate Answer Label
@IBOutlet weak var dripAnswer: UILabel!

My "Clear" button works, but I can't get the "Calculate" button to work. I'm trying to keep it simple for now and just add the two boxes, but i can't even get that to work.

Do2
  • 1,751
  • 1
  • 16
  • 28
beans217
  • 139
  • 2
  • 11
  • What crash do you get? – Paulw11 Sep 23 '16 at 02:57
  • It's a long list but basically singles my "calculatebutton" button as the reason – beans217 Sep 23 '16 at 03:01
  • That's not the reason. There will be a specific exception on a specific line. – Paulw11 Sep 23 '16 at 03:02
  • I only see a single textfield patientweight: UITextField! , where is the other one – gurmandeep Sep 23 '16 at 03:11
  • it pops up with this message "2016-09-22 23:14:05.082 FCEMS Protocols[1193:351411] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key calculatebutton. – beans217 Sep 23 '16 at 03:14
  • I took the second one out to make it more simple. But I can't even get only one to work. I figured If i can make one work, I can do the same thing for a second one. – beans217 Sep 23 '16 at 03:16
  • Check your storyboard connection again, i think you have did something wrong with the outlet connecting – Tj3n Sep 23 '16 at 03:32
  • I'm not sure how I am linking it up wrong. I'm just linking it up as an action – beans217 Sep 23 '16 at 03:57
  • That was it! something with the referencing solved the problem. Thank you everyone. – beans217 Sep 23 '16 at 04:13
  • Dont forget to implement my suggestions, if the user doesnt enter anything and presses calculate your app will crash! – Do2 Sep 23 '16 at 04:15
  • Thank you I just noticed it. Where can I find your suggestion? Any suggestion on what to enter in the swift file to take away the keyboard as well? – beans217 Sep 23 '16 at 04:17
  • this line will guard against crashing if the textfield is empty: ` guard let t = patientWeight.text, let weight = Double(t) else { return }` , alternative you could use` if let t = patientWeight.text {...}` – twiz_ Sep 23 '16 at 04:56
  • @beans217 i have posted an answer.. – Do2 Sep 23 '16 at 12:55

3 Answers3

0
    var weightpatient = Double(self.patientweight.text!)
    weightpatient = Double(weightpatient!*2)
    let basicStr:String = String(format:"%f", weightpatient!)
    dripanswer.text = String(basicStr)

Working fine..

Here is the demo link to https://www.dropbox.com/s/p67f88qnnsekivn/Blur.zip?dl=0 Try and let me know

gurmandeep
  • 1,227
  • 1
  • 14
  • 30
  • It's still crashing for me for some reason. I copied exactly what you typed under the IBaction. Have i possibly linked up something wrong? – beans217 Sep 23 '16 at 03:27
  • Updated the answer – gurmandeep Sep 23 '16 at 03:29
  • "2016-09-22 23:14:05.082 FCEMS Protocols[1193:351411] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key calculatebutton – beans217 Sep 23 '16 at 03:53
  • You are getting error? and check this link for your error http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v – gurmandeep Sep 23 '16 at 04:02
0

Maybe a "!" after weightpatient. Sub code is well on my simulator(xcode 7.3.1).

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var patientweight: UITextField!

  @IBAction func calculatebutton(sender: AnyObject) {
     let weightpatient = Double(self.patientweight.text!)

     dripanswer.text = String(weightpatient! * 2)

  }

  @IBAction func clearbutton(sender: AnyObject) {
     self.patientweight.text=nil
     //self.dosagedesired.text=nil
     self.dripanswer.text=nil
  }

  @IBOutlet weak var dripanswer: UILabel!

  override func viewDidLoad() {
     super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  }
}
Shashanth
  • 4,995
  • 7
  • 41
  • 51
Haixiao
  • 1
  • 4
0

First of all you should check if what is enter as patient weight is a number. Use this extension:

extension String {
var isNumber: Bool
{
    let range = self.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted)
    return (range == nil)
}
}

Then you check that the string is not empty and that it contains a number, if these 2 conditions are met you continue with the calculation..

 @IBAction func calculateButton(sender: AnyObject) {
  if patientWeight.text != "" && patientWeight.isNumber==true{
    let weightPatient = Double(self.patientWeight.text!)
    self.dripAnswer.text = String(weightPatient! * 2)
  }
  else{
  self.dripAnswer.text = "Please enter weight"
  }
 }
Do2
  • 1,751
  • 1
  • 16
  • 28