FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

BetterRest - init() deprecated

Forums > SwiftUI

Hello,

I'm following along with the Hacking with Swift: SwiftUI Edition and coding the calculateBedtime() method.

The book says to enter:

let model = SleepCalculator()

but this generates the following warning:

'init()' is deprecated: Use init(configuration:) instead and handle errors appropriately.

If I begin typing configuration and acept the autocompletion it generates:

let model = SleepCalculator(configuration: MLModelConfiguration)

but I've no idea what to provide!

Any help appreciated.

   

Change the line let model = SleepCalculator() to

let model: SleepCalculator = {
    do {
        let config = MLModelConfiguration()
        return try SleepCalculator(configuration: config)
    } catch {
        print(error)
        fatalError("Couldn't create SleepCalculator")
    }
}()

If you want you can handle the errors more gracefully, this will just crash the app if an error is thrown in the creation of the calculator.

1      

The solution that buddy supplied doesn't work

   

For the above solution, you need to import the CoreML library at the top of the file: import CoreML

Also, I am not a fan of immidiately invoked function expressions, so I would move the declaration of the model into the do/catch block like this:

do {
    let model: SleepCalculator = try SleepCalculator(configuration: MLModelConfiguration())
    let prediction = try model.prediction(wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount))

    // more code here
} catch {
    print(error)
    // something went wrong!
}

Hope this helps!

5      

Hope this helps, the following should work:

do {
    let model: SleepCalculator = try SleepCalculator(configuration: .init())
    let prediction = try model.prediction(wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount))

    // continue to process
} catch {
    // something went wrong!
}

   

@ksbex  

Thanks @vardyb, I just used your method to remove the warning on my project.

   

would it be better if make the model a private static constant property of the view struct?

private static let model: SleepCalculator = {
    do {
        return try SleepCalculator(configuration: .init())
    } catch {
        print(error)
        fatalError(String(localized: "Couldn't create SleepCalculator"))
    }
}()

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

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.