UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on 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.

3      

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.

4      

The solution that buddy supplied doesn't work

3      

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!

8      

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!
}

3      

@ksbex  

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

3      

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"))
    }
}()

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.