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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.