[英]Conversion to swift from objective-c
我目前正在尝试将Objective-C代码转换为openEars提供的示例应用程序。 但是,有这行代码:
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];
这是怎么迅速写的?
在框架中定义如下:
+ (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;
但是我确实尝试过这样的事情:
OEPocketsphinxController(TRUE, error: nil)
编译器错误为:
Swift编译器错误预期声明
您调用的Swift代码在Objective-C中看起来像这样:
[[OEPocketsphinxController alloc] initWith:YES error:nil]
有点...
您正在尝试调用不存在的构造函数。 相反,我们必须经历sharedInstance
:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
sharedInstance()
是一类方法OEPocketsphinxController
它返回的实例类OEPocketsphinxController
。
setActive(:error:)
是OEPocketsphinxController
类的实例方法,必须在此类的实例上调用。
因此,我们想使用sharedInstance()
获取一个实例,在该实例上调用setActive(:error:)
方法。
以下两段代码完全等效:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.