0

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

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

编辑:我能做些什么来代替这个?

import Speech
class ViewController2: UIViewController, SFSpeechRecognizerDelegate {

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

func doSomeStuffWithSpeech() {
...
}

...

}
4

1 回答 1

3

我已经高度集成 Speechkit

如果是这种情况,我认为创建两个单独的 viewController 可能更容易——或更合乎逻辑——你可以根据#available(iOS 10.0, *)

假设您将ViewController2基于点击另一个 ViewController 中的按钮来呈现(在代码片段中,我称之为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 {
        // ...
    }

    //...
}

但是,正如您所提到的,如果您与 Speechkit 进行了“高度”集成,我认为制作两个 Viewcontroller 会更合乎逻辑。

希望这有帮助。

于 2016-11-24T19:49:01.543 回答