繁体   English   中英

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

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

我正在尝试使用在Swift 2.2(XCode 7.3.1)中运行的RapidEars插件来运行OpenEars。 但是,我怀疑在Swift项目中使用带有扩展的Objective-C接口(或者我对其工作原理的理解)存在更大的问题。

OpenEars代码是Obj-C。 然而,通过标准的Obj-C - > Swift翻译技术,我能够在我的快速项目中运行它。

缩写代码如下。 完整的示例是在一个分叉的Github上并更新到Swift-2.2: https//github.com/SuperTango/OpenEars-with-Swift-

以下示例运行良好。 您可以通过检查“working-opears-swift2.2”标签来查看整个项目。

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:

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."
    }

到目前为止,一切都很好。

但是,为了使用“RapidEars”插件,文档( http://www.politepix.com/rapidears/ )说:

  • 将框架添加到项目中并确保正确包含它。
  • 导入两个新文件(既是现有OpenEars类的“类别”):

     #import <RapidEarsDemo/OEEventsObserver+RapidEars.h> #import <RapidEarsDemo/OEPocketsphinxController+RapidEars.h> 
  • 更改使用的方法: startListeningWithLanguageModelAtPath使用startRealtimeListeningWithLanguageModelAtPath

  • 添加两个新的OEEventsObservableDelegate方法。

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

通过从上面的github repo中查看rapidears-notworking-stackoverflow标记,可以找到新代码

问题1:

在XCode编辑器中完成后,编辑器会看到WILL在startRealtimeListeningWithLanguageModelAtPath方法上执行自动完成,但是当代码运行时,它总是失败并显示错误:

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

问题2:

在XCode编辑器中执行自动完成时,它没有看到RapidEarsDemo/OEPocketsphinxController+RapidEars.h定义的两个新委托方法。

我感觉这些是相关的,并且还与它们失败的方法被定义为Objective-C类的类别有关。 但这只是猜测。

我确保RapidEars框架已导入并在框架搜索路径中。

谁能告诉我为什么会这样? 或者,如果我错过了一些Swift魔法咒语?

问题可能是下面链接中描述的问题,其中静态库中的类别方法生成selector not recognized运行时错误。

技术问答QA1490:使用类别构建Objective-C静态库

暂无
暂无

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

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