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

Project 4 part 2 BetterRest predictions asking for a different data type

Forums > 100 Days of SwiftUI

I recreated the code of the instructor and the prediction model coffee data type Double(coffeeAmount) is asking for Int64 and not a Double. I can't figure why its doing this.

//
//  ContentView.swift
//  BetterRest
//
//  Created by Jonathan Loving on 6/22/23.
//

import CoreML
import SwiftUI

struct ContentView: View
{
    @State private var wakeUp = Date.now
    @State private var sleepAmount = 8.0
    @State private var coffeeAmount = 1

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

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

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

                Text("Daily coffeee intake")

                Stepper(coffeeAmount == 1 ? "1 cup" : "\(coffeeAmount) cups", value: $coffeeAmount, in: 1...20)
            }
            .navigationTitle("BetterRest")
            .toolbar
            {
                Button("Calculate", action: calculateBedtime)
            }
        }
    }

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

       } catch {
           //Something went wrong
       }
    }
}

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

Here is the specifice line of code that is having issues:

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

2      

I know this is an old post but I had the same issue today. It would seem that the ML class SleepCalculator expects an INT64 rather than a Double. So you need to convert your parameters (hour + minutes) and coffeeAmount to Int64.

Changing the code to:

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

worked for me.

It may also be possible to force constant <prediction> to be of type Double but I haven't tried that.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.