[英]Conversion to swift from objective-c
I'm currently trying to convert the objective-c code to swift of the sample app provided by openEars. 我目前正在尝试将Objective-C代码转换为openEars提供的示例应用程序。 However there is this one line of code :
但是,有这行代码:
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];
How is this written in swift? 这是怎么迅速写的?
It was defined like this in the framework: 在框架中定义如下:
+ (OEPocketsphinxController *)sharedInstance;
/**This needs to be called with the value TRUE before setting properties of OEPocketsphinxController for the first time in a session, and again before using OEPocketsphinxController in case it has been called with the value FALSE.*/
- (BOOL)setActive:(BOOL)active error:(NSError **)outError;
However I did try something like this: 但是我确实尝试过这样的事情:
OEPocketsphinxController(TRUE, error: nil)
The compiler error was: 编译器错误为:
Swift Compiler Error Expected declaration
Swift编译器错误预期声明
The Swift code you've called would look like this in Objective-C: 您调用的Swift代码在Objective-C中看起来像这样:
[[OEPocketsphinxController alloc] initWith:YES error:nil]
Sort of... 有点...
You're trying to call a constructor which does not exist. 您正在尝试调用不存在的构造函数。 Instead, we must go through the
sharedInstance
: 相反,我们必须经历
sharedInstance
:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
sharedInstance()
is a class method of the OEPocketsphinxController
class which returns an instance of the OEPocketsphinxController
. sharedInstance()
是一类方法OEPocketsphinxController
它返回的实例类OEPocketsphinxController
。
setActive(:error:)
is an instance method of the OEPocketsphinxController
class and must be called on an instance of this class. setActive(:error:)
是OEPocketsphinxController
类的实例方法,必须在此类的实例上调用。
So, we want to use sharedInstance()
to get an instance on which to call the setActive(:error:)
method on. 因此,我们想使用
sharedInstance()
获取一个实例,在该实例上调用setActive(:error:)
方法。
The following two pieces of code are exactly equivalent: 以下两段代码完全等效:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.