I was doing this tutorial. However even though I did everything according to the video, when I build and click the button, I get this error: Failed to get the home directory when checking model path. I checked that code and project files but I don't know how to fix this issue.
struct ContentView: View {
@State private var wakeUp = Date.now
@State private var sleepAmount = 8.0
@State private var coffeeAmount = 1
@State private var alertTitle = ""
@State private var alertMessage = ""
@State private var showingAlert = false
var body: some View {
NavigationStack{
VStack{
Text("When do you want to wake up?")
.font(.headline)
DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
.labelsHidden()
Text("Desired amount of sleep")
.font(.headline)
Stepper("\(sleepAmount.formatted()) hours", value: $sleepAmount, in: 4...12, step: 0.25)
Text("Daily coffee intake")
.font(.headline)
Stepper(coffeeAmount == 1 ? "1 cup" : "\(coffeeAmount) cups", value: $coffeeAmount, in: 1...20)
}
.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 = "Sorry, there was a problem about calculating your bedtime."
}
}
}