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

Error when loading BetterRest project 4 part 2 on iOS Simulator

Forums > 100 Days of SwiftUI

When loading the BetterRest Project 4 part 2 on the iOS simulator I get the following error: "Failed to get the home directory when checking model path." No clue how to get rid of this. I suppose it may have to do with changing the name to SleepCalculator maybe or maybe where I saved the coreml file? I am not sure but the app works it just spams my debugging area with that error. Please help.

BetterRest Xcode

Here is the code:

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

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 alertTitle = ""
    @State private var alertMessage = ""
    @State private var showingAlert = false

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

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

                VStack(alignment: .leading, spacing: 0)
                {
                    Text("Desired amout of sleep")
                        .font(.headline)

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

                VStack(alignment: .leading, spacing: 0)
                {
                    Text("Daily coffeee intake")

                    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 = "Error"
           alertMessage = "Sorry, there was a problem calculating your bedtime."
       }
        showingAlert = true
    }
}

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

2      

It seems like a lot of people have been running into this issue, and I haven't seen a real fix for it other than the work-around provided in the last response on this page https://developer.apple.com/forums/thread/720755

I get the same error when I try to run mine in the simulator, but I would just ignore it for now.

2      

I had the same thing but was able to fix it.

In the tutorial, it has you rename the model to SleepCalculator after exporting it from CreateML -- that may have somehting to do with it. I went back and made my CreateML file SleepCalculator.mlmodel, and in the xcode project I removed and then readded it. That seemed to resolve the "Failed to get home directory..." errors

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.