在 XCode 7 中导入带有类别扩展的接口时编译和运行时失败
Compile and runtime failures when importing interfaces with category extensions in XCode 7
我正在尝试在 Swift 2.2 (XCode 7.3.1) 中使用 RapidEars 插件 运行ning 获取 运行ning OpenEars 的示例。但是,我怀疑我在 Swift 项目中使用带有扩展的 Objective-C 接口有一个更大的问题(或者我对它如何工作的理解)。
OpenEars 代码是 Obj-C。然而,我能够通过标准的 Obj-C -> Swift 翻译技术在我的 swift 项目中得到它 运行ning。
缩写代码如下。完整示例在分叉 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/) 说:
- 将框架添加到项目中并确保它被正确包含。
导入两个新文件(都是 "categories" 到现有 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-Whosebug
标签找到
问题 1:
在 XCode 编辑器中完成时,编辑器会看到 WILL 在 startRealtimeListeningWithLanguageModelAtPath
方法上执行自动完成,但是当代码为 运行 时,它总是失败并出现错误:
[OEPocketsphinxController startRealtimeListeningWithLanguageModelAtPath:dictionaryAtPath:acousticModelAtPath:]: unrecognized selector sent to instance 0x7fa27a7310e0
问题 2:
在 XCode 编辑器中执行自动完成时,它看不到 RapidEarsDemo/OEPocketsphinxController+RapidEars.h
中定义的两个新委托方法。
我觉得这些是相关的,并且还与它们失败的方法被定义为 Objective-C 类 的类别这一事实有关。但这只是目前的猜测。
我已确保 RapidEars 框架已导入且位于框架搜索路径中。
谁能告诉我为什么会这样?或者如果我错过了一些 Swift 魔法咒语?
问题可能是下面 link 中描述的问题,其中静态库中的类别方法会产生 selector not recognized
运行时错误。
Technical Q&A QA1490: Building Objective-C static libraries with categories
我正在尝试在 Swift 2.2 (XCode 7.3.1) 中使用 RapidEars 插件 运行ning 获取 运行ning OpenEars 的示例。但是,我怀疑我在 Swift 项目中使用带有扩展的 Objective-C 接口有一个更大的问题(或者我对它如何工作的理解)。
OpenEars 代码是 Obj-C。然而,我能够通过标准的 Obj-C -> Swift 翻译技术在我的 swift 项目中得到它 运行ning。
缩写代码如下。完整示例在分叉 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/) 说:
- 将框架添加到项目中并确保它被正确包含。
导入两个新文件(都是 "categories" 到现有 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-Whosebug
标签找到
问题 1:
在 XCode 编辑器中完成时,编辑器会看到 WILL 在 startRealtimeListeningWithLanguageModelAtPath
方法上执行自动完成,但是当代码为 运行 时,它总是失败并出现错误:
[OEPocketsphinxController startRealtimeListeningWithLanguageModelAtPath:dictionaryAtPath:acousticModelAtPath:]: unrecognized selector sent to instance 0x7fa27a7310e0
问题 2:
在 XCode 编辑器中执行自动完成时,它看不到 RapidEarsDemo/OEPocketsphinxController+RapidEars.h
中定义的两个新委托方法。
我觉得这些是相关的,并且还与它们失败的方法被定义为 Objective-C 类 的类别这一事实有关。但这只是目前的猜测。
我已确保 RapidEars 框架已导入且位于框架搜索路径中。
谁能告诉我为什么会这样?或者如果我错过了一些 Swift 魔法咒语?
问题可能是下面 link 中描述的问题,其中静态库中的类别方法会产生 selector not recognized
运行时错误。
Technical Q&A QA1490: Building Objective-C static libraries with categories