UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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()
    }
}

2      

Remove

header: {
  Text("To")
}

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

3      

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

2      

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()
    }
}

2      

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.

2      

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

2      

Hacking with Swift is sponsored by RevenueCat

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.

Learn more here

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

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.