|
I have no idea why my code is not working. There are no errors or warnings. Result always shows 0. When i return input.value instead of output.value it works fine but it refuses to return output.value for some reason.
|
|
@moser poses a question:
When I have this issue, I first resign myself to the conclusion that it must be a PEBCAK error. Next I try the rubber duck method to peel back the uncertainty and try to focus on what may be the root of the problem. See -> Rubber Ducks π€ Teach Swift Let's see how you implemented your code. Using the Rubber Duck method, let's add comments....
We see above that printing the description of the input object shows both value (12) and units (ft). But, let's explore that a bit deeper. See --> Measurement Documentation Looking at the documentation for
Unit ObjectsYou provided a nice clean double value. But are you providing a PROPER Rubber duck this part! Do the letters "km" represent a proper Digging further into the documentation, Apple tells us that the Yikes! π€ It looks like you're not using existing, prebuilt measurements that know how to convert themselves. Instead you created a NEW Why might you do this? For example, you could create a new But inches, meters, yards, etc are common units of length and Swift has many prebuild For more info see @twoStraw's article: See --> Unit Conversions Keep CodingPlease return here and share how you solved this. |
|
The problem was me not reading the documentation properly after all... I added two switch statements to solve the problem. This is how my computed property looks like in the working code.
|
|
Your code appears to be mostly correct. However, there are a couple of minor issues and improvements you can make. Here's the corrected code: import SwiftUI struct ContentView: View { @State private var lengthInput = "" @State private var inputUnit = "m" @State private var outputUnit = "m"
} struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } Here are the changes made: Renamed lenghtIsFocused to lengthIsFocused for consistency and corrected its usage in the code. Added optional binding for converting the lengthInput to a Double and guarding against non-numeric input. Modified the Text view in the "Result" section to format the result with two decimal places using specifier: "%.2f". These changes should make your code work as expected. |
SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.
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.