我已经将Speechkit大量集成到我的应用程序的视图控制器中。Speechkit仅在iOS 10上可用,但我也需要我的应用程序在iOS 9设备上运行。
现在,我的应用程序在iOS9设备上启动时崩溃;如何防止Speechkit崩溃iOS9及更早版本?我可以创建两个单独的视图控制器文件,还是必须在每个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() {
...
}
...
}
最佳答案
我有高度集成的演讲工具包
如果是这样的话,我认为创建两个独立的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进行了“大量”集成,那么我假设让两个viewcontroller更符合逻辑。
希望这有帮助。