WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Updated Day 27 Questions & Issues. Failed build. Topic: Connecting SwiftUI to Core ML

Forums > 100 Days of SwiftUI

Hey everyone I hope you're doing well! Apologies in advance if what I write doesn't make sense.

I'm running into several issues, but I think I'll start with the basics of what tools I'm using:

Xcode version 15.3 iOS 17.4 simulator

Update 3/27/2024: I just wanted to let you know that the code given works fine after doing the challenge for day 28/removing the calculate button and making the recommended bedtime a var that returns the bed time text that is shown in the form.

Update 3/26/2024: I turned my Mac Off and On again and today I am still not able to rename the mlmodel file. (Yesterday was a Jen day. Today is more of a Roy day). I deleted the mlmodel and imported it again. I used the renamed SleepCalculator, but today the input is Double for all the input and output; not Int64. No idea why other than I restarted my Mac (hence it being a Roy day).

This line of code works now, and it builds, now that all the data fields are Double in the SleepCalculator:

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

However, it still won't show the alert message when I tap Calculate in the simulator.

Error: "Failed to get the home directory when checking model path."

I think what I need to do is start making the app over again and hopefully learn what I may be doing wrong.

~~I wasn't able to rename the BetterRest.mlmodel file to SleepCalculator.mlmodel in Xcode. I got a repository error so I deleted the file from the project then renamed the file SleepCalculator.mlmodel before import/drag & drop into the project. Not sure if that's part of the issue or not. I am not familiar with CoreML... or machine learning... or an expert at Xcode yet. ~~

These are the code and debug errors in func calculateBedtime:

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

Error: Cannot convert value '$wakeUp' of type 'Binding<Date>' to expected type 'Date', use wrapped value instead Remove '$'

Fix?: Changed $wakeUp to wakeUp New code: let components = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)

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

Error: Cannot convert value of type 'Double' to expected argument type 'Int64' Replace 'Double(hour + minute)' with 'Int64(Double(hour + minute))' Replace 'Double(coffeeAmount)' with 'Int64(Double(coffeeAmount))'

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

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

After those fixes the project will build but when I tap on calculate after entering the time to wake up, amount of sleep & coffee amount I get this notification in Xcode and no alert message in the simulator:

"Failed to get the home directory when checking model path."

Seems to be referencing this line of code: let sleepTime = wakeUp - prediction.actualSleep

After doing the 3rd video I'm also getting this error after adding the inflect code for cups, but the simulator works. LOL

Code: Stepper("^[(coffeeAmount) cup](inflect: true)", value: $coffeeAmount, in: 1...20)

Error: Cannot use inflection engine (Checking that the token is the correct type): (null)

Thanks in advance for taking the time to read this.

   

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.