简体   繁体   English

在XCode 7中导入具有类别扩展名的接口时编译和运行时失败

[英]Compile and runtime failures when importing interfaces with category extensions in XCode 7

I'm trying to get an example of running OpenEars with the RapidEars plugin running in Swift 2.2 (XCode 7.3.1). 我正在尝试使用在Swift 2.2(XCode 7.3.1)中运行的RapidEars插件来运行OpenEars。 However, I suspect I'm having a larger issue with using Objective-C interfaces with extensions in a Swift project (or my understanding of how that works). 但是,我怀疑在Swift项目中使用带有扩展的Objective-C接口(或者我对其工作原理的理解)存在更大的问题。

The OpenEars code is Obj-C. OpenEars代码是Obj-C。 However I was able to get it running in my swift project through the standard Obj-C -> Swift translation techniques. 然而,通过标准的Obj-C - > Swift翻译技术,我能够在我的快速项目中运行它。

Abbreviated code follows. 缩写代码如下。 The full example is on a forked Github and updated to Swift-2.2: https://github.com/SuperTango/OpenEars-with-Swift- 完整的示例是在一个分叉的Github上并更新到Swift-2.2: https//github.com/SuperTango/OpenEars-with-Swift-

This following example is working great. 以下示例运行良好。 You can see the entire project by checkng out the "working-opears-swift2.2" tag. 您可以通过检查“working-opears-swift2.2”标签来查看整个项目。

OpenEarsTest-Bridging-Header.h: OpenEarsTest桥接-Header.h:

#import <OpenEars/OELanguageModelGenerator.h>
#import <OpenEars/OEAcousticModel.h>
#import <OpenEars/OEPocketsphinxController.h>
#import <OpenEars/OEAcousticModel.h>
#import <OpenEars/OEEventsObserver.h>

ViewController.swift: ViewController.swift:

class ViewController: UIViewController, OEEventsObserverDelegate {

    var openEarsEventsObserver = OEEventsObserver()

    override func viewDidLoad() {
        super.viewDidLoad()
        loadOpenEars()
    }

    func loadOpenEars() {
        self.openEarsEventsObserver = OEEventsObserver()
        self.openEarsEventsObserver.delegate = self

        var lmGenerator: OELanguageModelGenerator = OELanguageModelGenerator()

        addWords()
        var name = "LanguageModelFileStarSaver"
        lmGenerator.generateLanguageModelFromArray(words, withFilesNamed: name, forAcousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"))

        lmPath = lmGenerator.pathToSuccessfullyGeneratedLanguageModelWithRequestedName(name)
        dicPath = lmGenerator.pathToSuccessfullyGeneratedDictionaryWithRequestedName(name)
    }

    func startListening() {
        do {
            try OEPocketsphinxController.sharedInstance().setActive(true)
            OEPocketsphinxController.sharedInstance().startListeningWithLanguageModelAtPath(lmPath, dictionaryAtPath: dicPath, acousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"), languageModelIsJSGF: false)
        } catch {
            NSLog("Error!")
        }
    }

    // A whole bunch more OEEventsObserverDelegate methods that are all working fine...
    func pocketsphinxDidStartListening() {
        print("Pocketsphinx is now listening.")
        statusTextView.text = "Pocketsphinx is now listening."
    }

Up until this point, everything is working great. 到目前为止,一切都很好。

However, In order to use the "RapidEars" plugin, the documentation ( http://www.politepix.com/rapidears/ ) says to: 但是,为了使用“RapidEars”插件,文档( http://www.politepix.com/rapidears/ )说:

  • Add the framework to the project and ensure it's being included properly. 将框架添加到项目中并确保正确包含它。
  • import two new files (that are both "categories" to existing OpenEars classes): 导入两个新文件(既是现有OpenEars类的“类别”):

     #import <RapidEarsDemo/OEEventsObserver+RapidEars.h> #import <RapidEarsDemo/OEPocketsphinxController+RapidEars.h> 
  • Change methods that used: startListeningWithLanguageModelAtPath to use startRealtimeListeningWithLanguageModelAtPath 更改使用的方法: startListeningWithLanguageModelAtPath使用startRealtimeListeningWithLanguageModelAtPath

  • add two new OEEventsObservableDelegate methods. 添加两个新的OEEventsObservableDelegate方法。

     func rapidEarsDidReceiveLiveSpeechHypothesis(hypothesis: String!, recognitionScore: String!) func rapidEarsDidReceiveFinishedSpeechHypothesis(hypothesis: String!, recognitionScore: String!) 

The new code can be found by checking out the rapidears-notworking-stackoverflow tag from the above github repo 通过从上面的github repo中查看rapidears-notworking-stackoverflow标记,可以找到新代码

Problem 1: 问题1:

When doing completion in the XCode editor, the editor sees WILL perform autocompletion on the startRealtimeListeningWithLanguageModelAtPath method, however when the code is run, it always fails with the error: 在XCode编辑器中完成后,编辑器会看到WILL在startRealtimeListeningWithLanguageModelAtPath方法上执行自动完成,但是当代码运行时,它总是失败并显示错误:

[OEPocketsphinxController startRealtimeListeningWithLanguageModelAtPath:dictionaryAtPath:acousticModelAtPath:]: unrecognized selector sent to instance 0x7fa27a7310e0

Problem 2: 问题2:

When doing auto completion in the XCode editor, it doesn't see the two new delegate methods defined in RapidEarsDemo/OEPocketsphinxController+RapidEars.h . 在XCode编辑器中执行自动完成时,它没有看到RapidEarsDemo/OEPocketsphinxController+RapidEars.h定义的两个新委托方法。

I have a feeling that these are related, and also related to the fact that they failing methods are defined as Categories to Objective-C classes. 我感觉这些是相关的,并且还与它们失败的方法被定义为Objective-C类的类别有关。 But that's only a guess at this point. 但这只是猜测。

I've made sure that the RapidEars framework is imported and in the framework search path. 我确保RapidEars框架已导入并在框架搜索路径中。

Can anyone tell me why this is happening? 谁能告诉我为什么会这样? Or if there's some Swift magic incantation that I missed? 或者,如果我错过了一些Swift魔法咒语?

The problem could be the one described in the link below, where category methods in a static library produce selector not recognized runtime errors. 问题可能是下面链接中描述的问题,其中静态库中的类别方法生成selector not recognized运行时错误。

Technical Q&A QA1490: Building Objective-C static libraries with categories 技术问答QA1490:使用类别构建Objective-C静态库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM