Posts

Post marked as solved
9 Replies
Thanks, i'll give this a try
Post marked as solved
9 Replies
Ah I see, that'd be why then. Is there any way I can extend the class to include it?
Post marked as solved
9 Replies
Yes that is what I tried originally, but it gives me this error:Type 'UnitDuration' has no member 'milliseconds'And it's the same error if I try and do the conversion from seconds.
Post marked as solved
9 Replies
I am trying to return a measurement value with the units "ms", similar to here which returns in hertz (Hz):else if length > 0 && length > (speed / 1000) { let resultFreq = Measurement(value: (Double(100*(speed / length))).rounded()/100, unit: UnitFrequency.hertz)for some reason UnitDuration. only lets me use seconds, minutes, hours. I'm wondering how to do a class extension to include milliseconds.
Post marked as solved
10 Replies
Yes this was very helpful and a much cleaner way than what I was doing, thanks!
Post marked as solved
10 Replies
Basically, I would like to have 3 texfields labelled as follows, with a calculate button below to return values into whichever fields the user wants to know.Frequency ...Wavelength ...Time Period ..."Calculate"- if the user enters a value in the frequency field, the wavelength = 343 / frequency and the time period = 1/frequency- if they enter the wavelength instead, the frequency = 343 / wavelength and the time period = wavelength / 343- if they enter the time period, the frequency = 1/time period and the wavelength = time period * 343With the help of the comment above (Claude31), I have managed to get two of the text boxes working properly with the following code, just need to add the 3rd text box for time period...import UIKitclass ViewController: UIViewController { @IBOutlet weak var frequencyField: UITextField! @IBOutlet weak var waveLengthField: UITextField! @IBOutlet weak var timePeriodField: UITextField! var freq = Float(0); var length = Float(0); 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 Calculate(_ sender: UIButton) { freq = Float(frequencyField.text!) ?? 0.0 length = Float(waveLengthField.text!) ?? 0.0 print(freq) // Just to see when they are 0 print(length) let speed = Float(343.0) if freq > 0 { // Avoid a zero div in some cases let resultWave = Float(speed / freq) waveLengthField.text! = String(resultWave) print(resultWave) } else { let resultWave = length waveLengthField.text! = String(resultWave) } if length > 0 { // Avoid a zero div in some cases let resultFreq = Float(speed / length) frequencyField.text! = String(resultFreq) } else { let freqPrint = freq frequencyField.text! = String(freqPrint) } } }
Post marked as solved
10 Replies
Yes managed to get it working with something similar to this, thank you! This was the code in the end:import UIKitclass ViewController: UIViewController { @IBOutlet weak var frequencyField: UITextField! @IBOutlet weak var waveLengthField: UITextField! @IBOutlet weak var timePeriodField: UITextField! var freq = Float(0); var length = Float(0); 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 Calculate(_ sender: UIButton) { freq = Float(frequencyField.text!) ?? 0.0 length = Float(waveLengthField.text!) ?? 0.0 print(freq) print(length) let speed = Float(343.0) if freq > 0 { let resultWave = Float(speed / freq) waveLengthField.text! = String(resultWave) print(resultWave) } else { let resultWave = length waveLengthField.text! = String(resultWave) } if length > 0 { let resultFreq = Float(speed / length) frequencyField.text! = String(resultFreq) } else { let freqPrint = freq frequencyField.text! = String(freqPrint) } } }
Post marked as solved
10 Replies
Just to confirm, is this the first option implemented correctly? I have added freq and length as variables in the class. Currently I can type into frequencyField and have it return the correct answer in waveLengthField. However, if I try and type anything in waveLengthField directly, the app crashes. I have commented out two lines which are the second part of the equation for now. If I include these two lines, it causes the values to change every time the button is pressed.Here's the full code:import UIKitclass ViewController: UIViewController { @IBOutlet weak var frequencyField: UITextField! @IBOutlet weak var waveLengthField: UITextField! @IBOutlet weak var timePeriodField: UITextField! var freq = Float(0); var length = Float(0); 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 Calculate(_ sender: UIButton) { freq = Float(frequencyField.text!) ?? 1 length = Float(waveLengthField.text!) ?? 1 //print(freq) //print(length) let speed = Float(343.0); var resultWave = Float(speed / freq) waveLengthField.text! = String(resultWave) //var resultFreq = Float (speed / length) I have commented out these two lines, but this is the second part of the equation. //frequencyField.text! = String(resultFreq) //frequencyField.text! = "\(resultFreq)" // waveLengthField.text! = String(resultWave) } }
Post marked as solved
10 Replies
Thanks for the reply. I tried the first option but still got "inf" in the frequency text field. I then tried the 2nd option but keep getting this error "cannot assign value of type string to type float" when I attempt to assign the the sender text to variable "length". I can see the numbers printing when I type into the wavelength texfield, but can't seem to unwrap / assign them to a var / float.@IBAction func waveLengthEdited(_ sender: UITextField) { print(sender.text!) // I can see the correct numbers printing when I type. But not when I return them from the calculation, it // always returns to 0. length = (sender.text!) if Float(sender.text!) != nil { length = (sender.text!) } else { freq = 0.0 sender.text = "no value" } } @IBAction func Calculate(_ sender: UIButton) { let speed = Float(343.0); let resultWave = speed / freq let resultFreq = speed / length frequencyField.text! = "\(resultFreq)" waveLengthField.text! = "\(resultWave)" }
Post marked as solved
4 Replies
Great, thanks!
Post marked as solved
4 Replies
Thanks this has helped, I have managed to get further. However, when I enter a value into box 1, it gives me the answer for box 2 (great). But then the value in box 1 changes to "inf". It seems I have created some sort of loop. I have tried getting around it with an if statement but not had much luck. This is the code: @IBAction func Calculate(_ sender: UIButton) { let Freq = Float(FrequencyField.text!) ?? 0 let Length = Float(WaveLengthField.text!) ?? 0 let Speed = Float(343.0); let resultWave = Speed / Freq let resultFreq = Speed / Length print(resultFreq) WaveLengthField.text = String(resultWave) FrequencyField.text = String(resultFreq) }
Post marked as solved
2 Replies
Yes this was it, thanks!