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

SOLVED: Help BetterRest Wrap Up

Forums > 100 Days of SwiftUI

Hi! I've refactored the project as per specifications but I can't solve the line errors, can you help me?

Here's the code:

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 recommendedBedtime = bedTime

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

    static var bedTime: String {
        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: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount))

            let sleepTime = wakeUp - prediction.actualSleep

            recommendedBedtime = sleepTime.formatted(date: .omitted, time: .shortened)

        } catch {
            // Something went wrong
        }
        return recommendedBedtime
    }

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

                    Section("Desired amount of sleep") {
                        Stepper("\(sleepAmount.formatted()) hours", value: $sleepAmount, in: 4...12, step: 0.25)
                    }

                    Picker(coffeeAmount == 1 ? "1 cup" : "\(coffeeAmount) cups", selection: $coffeeAmount) {
                        ForEach(1...20, id: \.self) { number in
                            Text("\(number) cup")
                        }
                    }
                    Section() {
                        Text("Recommended bedtime: \(recommendedBedtime)")
                            .font(.headline)
                    }
                }
                .navigationTitle("BetterRest")
            }
        }
    }
}

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

Thanks

2      

Try and return sleepTime.formatted(date: .omitted, time: .shortened) and remove the last line return recommendedBedtime

you might want put a fatelError(message) in the catch

sorry do not have my computer with my so do this on iPad but think that should work as you not shown what error you are getting.

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.