A warning “'init()' is deprecated”. [Swift, Ios app, learning model]

人走茶凉 提交于 2020-11-29 18:56:43

问题


I am making a image classification iOS app with Swift.

When I write

guard let model = try? VNCoreMLModel(for: SqueezeNet().model) else { return }

I got this warning.

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

Do you know how to get rid of it?


回答1:


As the message notes, init() is deprecated. The new initializer takes a configuration parameter. The default configuration may be fine for you, in which case you would replace SqueezeNet() with:

SqueezeNet(configuration: MLModelConfiguration())



回答2:


That's odd. Right click SqueezeNet() and jump to it's definition. It will take you to the class.

Find the init() method of the class. It should look like this within your SqueezeNet class:

/**
    Construct SqueezeNet instance by automatically loading the model from the app's bundle.
*/
@available(*, deprecated, message: "Use init(configuration:) instead and handle errors appropriately.")
convenience init() {
    try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
}

I'm not sure how you set up your ML, but it appears as though the:

  • @available(*, deprecated, message: "Use init(configuration:) instead and handle errors appropriately.")

Isn't passing for you. This could mean any of the following:

  1. You set up your ML incorrectly
  2. Your iOS isn't up to date

Easy Fix:

All you have to do is paste this into your project:

extension SqueezeNet {
    convenience init(_ foo: Void) {
        try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
    }
}

Then, edit your code like this:

guard let model = try? VNCoreMLModel(for: SqueezeNet(()).model) else { return }

It should no work fine. Please let me know if it doesn't.



来源:https://stackoverflow.com/questions/64648967/a-warning-init-is-deprecated-swift-ios-app-learning-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!