Reputation: 65
Below is some very basic code for your SO experts. I have two fields (weight and height) and a segmented control with two options. When the control is clicked, I fire an action to get the values of the weight and height fields. I then do a small amount of math and am looking to dump the result to another label field (result). Whenever entries are calculated the results field is populated with "NaN". Any ideas as to why? I'm sure it is in some formatting or conversion somewhere.
Edit: Updated to specify the issue.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var WeightNumberTextField: UITextField!
@IBOutlet weak var HeightNumberTextField: UITextField!
@IBOutlet weak var CalculateType: UISegmentedControl!
@IBOutlet weak var results: UILabel!
var weight = Float()
var height = Float()
var BMI = Float()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func newCalculation(sender: UISegmentedControl) {
switch CalculateType.selectedSegmentIndex {
case 0:
let BMI = (weight * 703) / height;
results.text = BMI.description;
case 1:
let BMI = weight / height;
results.text = BMI.description;
default:
break
}
}
}
Upvotes: 0
Views: 2114
Reputation: 2241
if it's breaking at a "breakpoint" then somewhere in your code you'll have set one. This is a point in your code that the Xcode debugger stops (well pauses) for you to do various things to the code using the debugger etc...
You can see where a breakpoint is in one of several ways:
1) Using the "Breakpoint Navigator" which appears in the left-most pane of Xcode under a tab which appears like this:
2) By looking for breakpoints in the gutter of your code. In the actual code pane, where all your code is, you'll see just left of it a "gutter" which could appear in a number of ways depending how you've got Xcode set up... Mine appears as follows (with a breakpoint):
note) Where the arrow is pointing is where the breakpoint is. i.e. the highlighted bit in blue. You can enable and disable them by just clicking on them... If it's disabled, it'll appear "Greyed out" i.e. like this:
Finally, in regards to referring to the text field "filled in values" and the "unsegmented control action", you need to:
i) Textfield values:
// Somewhere in a code-block (i.e. a function)
let height = self.HeightNumberTextField.text
// Do some validation i.e. is it a number, is it greater than etc...
ii) Reference to the segmented control from the action:
// As you can see there's (sender: UISegmentedControl) which
// is the segmented control that you're clicking on so your
// switch statement should be..
switch sender.selectedSegmentIndex
sender (applies to all controls) is the control which performed the action i.e. in this case, the UISegmentedControl which you've tapped on. This is especially handy if you have several UISegmentedControls referring to one action.
Hope this helps!
edit Just something to add, I'd personally have "default values" on both of the textfields to be "0" so when the view loads, there's at least something there. Also I'd move the actual BMI calc to another function and refer to it from the action rather than work it out in the action.
Upvotes: 0
Reputation: 385540
The weight
and height
fields are initialized to 0. You never modify them. Dividing 0 / 0 in floating point returns NaN (not-a-number).
You probably want something like this:
@IBAction func newCalculation(sender: UISegmentedControl) {
weight = Float(WeightNumberTextField.text ?? "0") ?? 0
height = Float(HeightNumberTextField.text ?? "0") ?? 0
switch CalculateType.selectedSegmentIndex {
case 0:
let BMI = (weight * 703) / height;
results.text = BMI.description;
case 1:
let BMI = weight / height;
results.text = BMI.description;
default:
break
}
}
Upvotes: 6