NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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.

1      

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.

2      

The solution that buddy supplied doesn't work

1      

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!

6      

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

1      

@ksbex  

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

1      

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

1      

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.