阻止 Speechkit 崩溃 iOS 9 台设备

Stopping Speechkit from crashing iOS 9 devices

我已将 Speechkit 大量集成到我的一个应用程序的视图控制器中。 Speechkit 仅在 iOS 10 上可用,但我还需要我的应用程序在 iOS 9 台设备上 运行。

现在,我的应用程序在 iOS 9 台设备上启动时崩溃;如何防止 Speechkit 使 iOS 版本 9 及更早版本崩溃?我可以创建两个单独的视图控制器文件,还是必须在每个 Speechkit 引用周围放置 if #available(iOS 10, *) {

编辑:除了这个我还能做什么?

import Speech
class ViewController2: UIViewController, SFSpeechRecognizerDelegate {

if #available(iOS 9, *) { // ERROR: Expected Declaration
private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
}

func doSomeStuffWithSpeech() {
...
}

...

}

I have heavily integrated Speechkit

如果是这样,我认为创建两个独立的 viewController 可能更容易 - 或者更合乎逻辑 - 您可以根据 #available(iOS 10.0, *)

决定应该查看哪个

假设您将根据点击另一个 ViewController 中的按钮显示 ViewController2(在代码片段中,我称之为 PreviousViewController):

class PreviousViewController: UIViewController {
    //...

    @IBAction func presentApproriateScene(sender: AnyObject) {
        if #available(iOS 10.0, *) {
            // present the ViewController that heavily integrated with Speechkit
            // maybe by perfroming a segue:
            performSegueWithIdentifier("segue01", sender: self)

            // or maybe by getting the it from the storyboard
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc1 = storyboard.instantiateViewControllerWithIdentifier("vc1")
            presentViewController(vc1, animated: true, completion: nil)

        } else {
            // present the ViewController that does not suupport Speechkit
            // maybe by perfroming a segue:
            performSegueWithIdentifier("segue02", sender: self)

            // or maybe by getting the it from the storyboard
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc2 = storyboard.instantiateViewControllerWithIdentifier("vc2")
            presentViewController(vc2, animated: true, completion: nil)
        }
    }

    //...
}

还有,可以在声明变量的时候使用:

class ViewController: UIViewController {
    //...

    if #available(iOS 10.0, *) {
        private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
    } else {
        // ...
    }

    //...
}

但同样,正如您提到的,如果您 "heavy" 与 Speechkit 集成,我认为制作两个 Viewcontroller 会更合乎逻辑。

希望对您有所帮助。