2016-11-24 60 views
0

我的应用程序的视图控制器中集成了高度集成的Speechkit。 Speechkit仅适用于iOS 10,但我也需要我的应用程序在iOS 9设备上运行。停止使用iOS 9设备崩溃的Speechkit

现在,我的应用程序在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() { 
... 
} 

... 

} 
+0

可能,您应该使用#available(iOS 10,*),如您所述。 –

+0

@AhmadF当我在课堂上定义变量时呢?我只是没有弄清楚在那里做什么。 – owlswipe

+0

“班级变数”的问题在哪里?我建议添加一个代码片段以保持清晰。 –

回答

3

我有严重的集成Speechkit

如果是这样的,我想创建两个分离viewControllers可能是更容易的情况 - 或者更logical-,你可以决定哪一个应该被观看基础上, #available(iOS 10.0, *)

让我们假设你将呈现基于另一个视图控制器按一个按钮就在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会更合乎逻辑。

希望这有助于。

+0

所以刚过一段时间之后,如果我在Speechkit视图控制器中拥有IBOutlet(与故事板绑定),并且如果这是主视图控制器,而不是从另一个视图控制器中寻找到的,我怎么能这样做呢? – owlswipe

+0

我没有得到它:)你的意思是它是最初的ViewController? –

+0

是的,初始视图控制器 – owlswipe

相关问题