0

I'm trying to create a conversion from Fahrenheit to Celsius, which requires me to call on the amount the user chose. Here's what I have so far

@State private var temperatureChoices = ["Celsius", "Fahrenheit"]
     @State private var startTemp = "Celsius"
     @State private var endTemp = "Fahrenheit"
     @State private var startAmount = 0.0
     @State private var endAmount = 0.0
     let fToC = {
         return (5/9) * (startAmount - 32)
     }
var body: some view{

I've tried doing it without the closure, but that doesn't work either. I've tried doing it inside of the body, but that doesn't work. What am I doing wrong here? How do I instantiate a variable using another variable?

5
  • You want a computed property, for fToC change it from let to var and remove the =. Commented Nov 6, 2022 at 23:13
  • I don't quite understand what you are doing, but looks weird to me that you have @State for every option. I would expect 1 state property for type of temperature (e.g. @State private var selectedType: String // "Celsius" or "Fahrenheit") and one @State for current selection (e.g. @State private var amount: Double // whatever user chose, in selectedType units). Also consider using enum to express "Celsius", "Fahrenheit" Commented Nov 6, 2022 at 23:16
  • @George I get two warnings when I do that. Computed property must have an explicit type and Type annotation missing in pattern Commented Nov 6, 2022 at 23:23
  • 2
    For conversion between celsius and fahrenheit, you can use the Measurement API in Swift. Commented Nov 7, 2022 at 1:17
  • try this: var fToC: Double { (5/9) * (startAmount - 32) } Commented Nov 7, 2022 at 2:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.