TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Issues with Form on Day 28 challenge BetterRest

Forums > 100 Days of SwiftUI

When I try to run my code I get the following error: Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure. I am certain its from the picker I am using in the code. How do I fix this?

Here is the code:

//
//  ContentView.swift
//  BetterRest
//

import CoreML
import SwiftUI

struct ContentView: View
{
    @State private var wakeUp = defaultWakeTime
    @State private var sleepAmount = 8.0
    @State private var coffeeAmount = 1

    @State private var alertTitle = ""
    @State private var alertMessage = ""
    @State private var showingAlert = false

    static var defaultWakeTime: Date
    {
        var components = DateComponents()
        components.hour = 7
        components.minute = 0
        return Calendar.current.date(from: components) ?? Date.now
    }

    var body: some View
    {
        NavigationView
        {
            Form
            {
                Section
                {
                    Text("When do you want to wake up")
                        .font(.headline)
                    DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
                        .labelsHidden()
                }

                Section
                {
                    Text("Desired amout of sleep")
                        .font(.headline)

                    Stepper("\(sleepAmount.formatted()) hours", value: $sleepAmount, in: 4...12, step: 0.25)
                }

                Section
                {
//                    Text("Daily coffeee intake")
//                        .font(.headline)

                    Picker("Daily coffee intake", selection: $coffeeAmount)
                        .font(.headline)
                    {
                        ForEach(1..<21)
                        { myCoffeeAmount in
                            Text(myCoffeeAmount == 1 ? "1 cup" : "\(myCoffeeAmount) cups")
                        }
                    }
                }
            }
            .navigationTitle("BetterRest")
            .toolbar
            {
                Button("Calculate", action: calculateBedtime)
            }
            .alert(alertTitle, isPresented: $showingAlert) {
                Button("OK") { }
            } message: {
                Text(alertMessage)
            }
        }
    }

    func calculateBedtime()
    {
       do
       {
           let config = MLModelConfiguration()
           let model = try SleepCalculator(configuration: config)

           let components = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)

           let hour = (components.hour ?? 0) * 60 * 60
           let minute = (components.minute ?? 0) * 60

           let prediction = try model.prediction(wake: Int64(hour + minute), estimatedSleep: sleepAmount, coffee: Int64(coffeeAmount))

           let sleepTime = wakeUp - prediction.actualSleep
           alertTitle = "Your ideal bedtime is..."
           alertMessage = sleepTime.formatted(date: .omitted, time: .shortened)

       } catch {
           alertTitle = "Error"
           alertMessage = "Sorry, there was a problem calculating your bedtime."
       }
        showingAlert = true
    }
}

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

Here is the line of code giving me problems:

Section
                {

                    Picker("Daily coffee intake", selection: $coffeeAmount)
                        .font(.headline)
                    {
                        ForEach(1..<21)
                        { myCoffeeAmount in
                            Text(myCoffeeAmount == 1 ? "1 cup" : "\(myCoffeeAmount) cups")
                        }
                    }
                }

2      

I tried changing the picker to this but it still does not work:

Section
                {

                    Picker("Daily coffee intake", selection: $coffeeAmount)
                        .font(.headline)
                    {
                        ForEach(1..<21)
                        {
                            Text($0 == 1 ? "1 cup" : "\($0) cups")
                        }
                    }
                }

2      

So when I add a pickerstyle to the code it compiles but it still gives me the following warning: 1. 'init(_:)' declared here (SwiftUI.Form)

Also my code starts with 2 cups of coffee instead of just one. Not sure why its doing this.

Section
                {

                    Picker("Daily coffee intake", selection: $coffeeAmount)
                    {
                        ForEach(1..<21)
                        {
                            Text($0 == 1 ? "1 cup" : "\($0) cups")
                        }
                    }
                    .pickerStyle(.automatic)
                    .font(.headline)
                }

2      

When I change ForEach to (0..<21) I start at 1 cup but when I use ForEach(1..<21) it starts at 2.

2      

Hi, so as you probably noticed you cannot apply font modifier to the picker like so:

 Picker("Daily coffee intake", selection: $coffeeAmount)
                        .font(.headline)

but only after a closure it takes:

Picker("Daily coffee intake", selection: $coffeeAmount)
                    {
                        ForEach(1..<21)
                        {
                            Text($0 == 1 ? "1 cup" : "\($0) cups")
                        }
                    }
                    .pickerStyle(.automatic)
                    .font(.headline) // <- font modifier applied here

To solve the second issue the easiest way is to create array of numbers after your @State properties like that:

let numberOfCoffeeCups = Array(1...20)

and use it in your picker similar to this.

Picker("Number of cups", selection: $coffeeAmount) {
                        ForEach(numberOfCoffeeCups, id: \.self) {
                            Text($0, format: .number)
                        }
                    }

2      

What about this error? What do I do about it?

  1. 'init(_:)' declared here (SwiftUI.Form),

The error happens after my first form after navigation view.

Error in Xcode

2      

Here is the code of the solution of your problem

{

                Picker("Daily coffee intake", selection: $coffeeAmount)
                    .font(.headline)
                {
                    ForEach(1..<21)
                    { myCoffeeAmount in
                        Text(myCoffeeAmount == 1 ? "1 cup" : "\(myCoffeeAmount) cups")
                    }
                }
            }

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!

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.