NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Day 19 challenge. Error: Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure

Forums > 100 Days of SwiftUI

Hi there!

Im trying to create Temperature converter in day 19 but im getting an error on Form: Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure

I know there is still missing mathematics but for now i just want to see how it looks :) Thanks


struct ContentView: View {

    @State private var inputValue = 0.0
    @State private var inputUnit = "Celsius"
    @State private var outputUnit = "Kelvin"

    let temperatureUnits = ["Celsius", "Kelvin", "Fahrenheit"]

    var body: some View {
        NavigationView {
            Form { 
                Section {
                    Picker("Temperature Unit", selection: $inputUnit) {
                        ForEach(temperatureUnits, id: \.self) {
                            Text($0)
                        }
                    } header: {
                        Text("From")
                    }

                    Picker("Temperature Unit", selection: $outputUnit) {
                        ForEach(temperatureUnits, id: \.self) {
                            Text($0)
                        }
                    } header: {
                        Text("To")
                    }
                }

                Section {
                    TextField("Enter value", value: $inputValue,format: .number)
                }

                Section {
                    Text(inputValue, format: .number)
                }
            }
            .navigationTitle("Unit Converter")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

   

Remove

header: {
  Text("To")
}

from your Pickers. These don't exist. They exist with Section though.

1      

Thanks @Hatsushira! Right, they should be with section. Accually i dont need them cause i changed Pickers names and that's ok :)

   

Hi again..

Im stuck with this calculations in calculateTemperature and now i only have chaos in my head...

Can you take a look at me code and maybe give me some hint?

//
//  ContentView.swift
//  Convert
//
//  Created by Filip Pokłosiewicz on 16/01/2023.
//

import SwiftUI

struct ContentView: View {

    @State private var inputValue = 0.0
    @State private var inputUnit = "Celsius"
    @State private var outputUnit = "Kelvin"

    let temperatureUnits = ["Celsius", "Kelvin", "Fahrenheit"]

    var calculateTemperature: Double {

        let celsius = inputValue
        var kelvin = celsius + 273.15
        var fahrenheit = Double(celsius 9/5) + 32

        return 0
    }

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("From", selection: $inputUnit) {
                        ForEach(temperatureUnits, id: \.self) {
                            Text($0)
                        }
                    }

                    Picker("To", selection: $outputUnit) {
                        ForEach(temperatureUnits, id: \.self) {
                            Text($0)
                        }
                    }
                }

                Section {
                    TextField("Enter temperature", value: $inputValue,format: .number)
                    .keyboardType(.decimalPad)
                } header: {
                    Text("Enter your temerature in \(inputUnit)")
                }

                Section {
                    Text(inputValue, format: .number)
                }
            }
            .navigationTitle("Unit Converter")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

   

Why are you calculating a value for calculateTemperature but then just returning 0 regardless of the result of your calculations?

Also, you should rename calculateTemperature. Names that start with a verb, like calculate, are more appropriate for a function. This is a computed property so it should be named like a property. Something like calculatedTemperature or convertedTrmperature would be much better.

   

Hi!

Ok i've changed a little this calculations but still its hard for me what mathematics should be in resultTemperature

    var calculatedTemperature: Double {

        let celsius = inputValue
        var kelvin = celsius + 273.15
        var fahrenheit = (celsius * 9/5) + 32

        let resultTemperature = what here??

        return resultTemperature
    }

I also dont know how swift will be able to convert it all without the rest of the formulas: https://ibb.co/Wp7Cp7r

   

@Filip

Please note. Your first question was answered then marked correct.

Now, you have a second question unrelated to the first. It has NOT been answered yet.
However, it's confusing because this thread is marked "Solved"

To reduce confusion, please start a new topic with your second question.

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.