我将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() {
...
}
...
}
答案 0 :(得分:3)
我有大量集成的Speechkit
如果是这样,我认为创建两个独立的viewControllers可能更容易 - 或者更符合逻辑 - 您可以根据#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 {
// ...
}
//...
}
但是,正如你所提到的,如果你有"沉重的"与Speechkit集成,我假设制作两个Viewcontrollers更合乎逻辑。
希望这会有所帮助。