【问题标题】:Compile and runtime failures when importing interfaces with category extensions in XCode 7在 XCode 7 中导入具有类别扩展的接口时编译和运行时失败
【发布时间】:2016-05-12 00:20:31
【问题描述】:

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

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

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

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

OpenEarsTest-Bridging-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 标签找到新代码

问题一:

在 XCode 编辑器中进行补全时,编辑器看到将在 startRealtimeListeningWithLanguageModelAtPath 方法上执行自动补全,但是当代码运行时,它总是失败并出现错误:

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

问题2:

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

我觉得这些是相关的,也与它们失败的方法被定义为 Objective-C 类的类别这一事实有关。但这只是目前的猜测。

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

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

【问题讨论】:

标签: ios objective-c xcode swift openears


【解决方案1】:

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

Technical Q&A QA1490: Building Objective-C static libraries with categories

【讨论】:

  • 是的。这修复了它(运行时异常部分)。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 2017-03-14
相关资源
最近更新 更多