[英]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
標記,可以找到新代碼
在XCode編輯器中完成后,編輯器會看到WILL在startRealtimeListeningWithLanguageModelAtPath
方法上執行自動完成,但是當代碼運行時,它總是失敗並顯示錯誤:
[OEPocketsphinxController startRealtimeListeningWithLanguageModelAtPath:dictionaryAtPath:acousticModelAtPath:]: unrecognized selector sent to instance 0x7fa27a7310e0
在XCode編輯器中執行自動完成時,它沒有看到RapidEarsDemo/OEPocketsphinxController+RapidEars.h
定義的兩個新委托方法。
我感覺這些是相關的,並且還與它們失敗的方法被定義為Objective-C類的類別有關。 但這只是猜測。
我確保RapidEars框架已導入並在框架搜索路徑中。
誰能告訴我為什么會這樣? 或者,如果我錯過了一些Swift魔法咒語?
問題可能是下面鏈接中描述的問題,其中靜態庫中的類別方法生成selector not recognized
運行時錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.