Posts

Post not yet marked as solved
2 Replies
1.1k Views
Hi there, I am trying to write part of a simple code to return either Celcius or Fahrenheit using a segemented control to switch between the two. If I enter a value into the textbox and switch the segmented control, it works great. However, when I try and switch back to the original case, it acts as if there is no text in the textfield and returns to the default value, 0.0. Essentially, I'd like it to check the textfield for text, not just require new text to be entered. Thanks in advance. Here's the code:@IBAction func tempConvert(_ sender: UISegmentedControl) { switch sender.selectedSegmentIndex { case 0: temperature = Float(temperatureField.text!) ?? 0.0 let celsius = Measurement(value: (Double(100*((temperature - 32) / 1.8).rounded() / 100)), unit:UnitTemperature.celsius) print(celsius) temperatureField.text = String(describing: celsius) checkTempUnits = true case 1: temperature = Float(temperatureField.text!) ?? 0.0 let fahrenheit = Measurement(value: (Double(100*((temperature * 1.8) + 32).rounded()/100)), unit: UnitTemperature.fahrenheit) temperatureField.text = String(describing: fahrenheit) checkTempUnits = false default: speed = Float(331 + (0.6 * temperature)) } }
Posted
by JMcEvoy.
Last updated
.
Post marked as solved
9 Replies
2.3k Views
The class UnitDuration lets you use seconds, minutes and hours, but not milliseconds.On the apple site I found this in relation to it: @NSCopying class var milliseconds: UnitDuration { get }How can I implement this into my code? (I am new to swift)Thanks in advance
Posted
by JMcEvoy.
Last updated
.
Post marked as solved
10 Replies
1.7k Views
I am new to xcode and attempting to make a simple equation calculator, consisting of 3 UITextFields which allows the user to enter a number into any of the fields and recieve an output in the other two fields.However, when I enter a value for the "frequencyField.text" and press the calculate button, the correct answer is displayed in the "waveLengthField" but it prints as 0. The next time I press calculate it treats "length" as 0 as there is no user entered text. I would like the waveLengthField to update with the returned value "resultWave". Here's the code: @IBAction func Calculate(_ sender: UIButton) { var freq = Float(frequencyField.text!) ?? 0 var length = Float(waveLengthField.text!) ?? 0 let speed = Float(343.0); let resultWave = "\(speed / freq) m" let resultFreq = speed / length print(length) print(freq) print(speed) frequencyField.text = String(resultFreq) waveLengthField.text = String(resultWave) }Thanks
Posted
by JMcEvoy.
Last updated
.
Post marked as solved
4 Replies
3.7k Views
Hi There, I am new to swift / xcode and am trying to create a simple equation calculator based on 3 variables. I have added 3 UITextFields where the user enters any one of the parameters and then presses a UIButton to calculate. I would like the answer to return in the same UITextFields used to enter the initial values, is this possible?So for example:The user enters a value in box 1 and gets results in box 2 and 3. But then they can also enter a value in box 3 and see results in boxes 1 and 2.Any help would be much appreciated, thanks. J
Posted
by JMcEvoy.
Last updated
.
Post marked as solved
2 Replies
530 Views
Hi there, I'm new to Xcode so this may be a simple thing that i'm missing. I am following a tutorial online, just trying to create two simple buttons and use a constraint to position the buttons 10 points apart from each other. However, when I add the constraint, the button doesn't move. I have tried clicking the "Update Frame" icon at the bottom of the workspace. Also I have tried going Editor > Resolve Auto-Layout Issues > Update Frames but still nothing. Any ideas? I am on macOS Sierra 10.12.6, using Xcode 9.2.ThanksJ
Posted
by JMcEvoy.
Last updated
.