|
First and foremost, a big, BIG "Thank you!" to Paul Hudson for creating such an extensive learning site for Swift. The last time I wrote an app was for MS-DOS in Turbo Pascal. I have what I think is a great idea for a Mac app, but first need to educate myself in Swift. Here's the first challenge app with the final code. It includes the challenge changes made to WeSplit. import SwiftUI struct ContentView: View { @State private var checkAmount = "" @State private var numberOfPeople = 2 @State private var tipPercentage = 2 @State private var piggyPeople = ""
} struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } This all works as expected. I went on then to the first challenge (Day 19). For the life of me, I can't figure out why the picker row doesn't work, or why the variable always remains as an empty string instead of being populated with "Celcius", "Fahrenheit" or "Kelvin". Because it's always empty, the func returns nothing. You can change any == in the func to "" and then the app will always return that calculated value, proving the variable tempType is always empty. Here's my code. What's wrong with it that the picker won't work, and the variable is always empty? Thanks in advance for looking. import SwiftUI struct ContentView: View { @State private var currentTemp = "" @State private var convertValue = 0.0 @State private var tempType = "" @State private var chosenConversion = 0.0
} struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
|
Hi A few things
I had
PS it usually to put properties before the body and function below. It just make it easier to read. |
|
Thank you, Nigel! I hate when the solution is looking you right in the face, but your brain is stuck on trying to do it one way. I spent three days trying to pull the string out of the array for my == conditions when your fix of using the much easier array position did the trick. You can also see my Pascal conditioning in action there. The "body" in Pascal is the main app code, and all functions, structs (procedures in Pascal) must go above the body so the app can find them. I'm still not used to the ideas you can:
At least now I can move on to project 2, and also the knowledge this forum is a great place to avoid doing the "lone wolf" learning Paul suggests against. I just didn't personally know anyone else programming for the Mac. Not even Facebook friends. |
|
Hi I have been thinking about it and if you want
PS i change the function to use I noticed that you have to enter in Fahrenheit. Are you going to do so the user can enter which input unit he want and the output unit? |
|
OR you can use
Then
The choise is yours! As you can see more then one way to skin a code 😂 |
|
Haha! Yes, I thought about making the func use a switch statement, too. Cleaner and faster to write than a bunch of "if this do that" choices. But I was already done writing the "ifs", so left it as is. I did mull over the silliness of having a choice for Fahrenheight when that's what the beginning value represents. It would have made more sense for the first input field to ask for a Fahrenheight value, and then have buttons only for conversions to Celcius and Kelvin. Which I see you did in another version. One other issue the first project didn't address was negative values. You were directed to use the proper numeric keypads, which makes it impossible to enter a negative symbol on an actual iOS device. Unless there's another besides .decimalPad and .numberPad that gives you one. And … yes, there is. I replaced .decimalPad with .numbersAndPunctuation. Puts more items you can tap on the screen that having nothing to do with a numeric value, but at least the user can then enter a negative value. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.