从 objective-c 转换为 swift
Conversion to swift from objective-c
我目前正在尝试将 objective-c 代码转换为 openEars 提供的示例应用程序的 swift。但是只有这一行代码:
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];
这个swift是怎么写的?
在框架中是这样定义的:
+ (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 Compiler Error Expected declaration
您调用的 Swift 代码在 Objective-C 中看起来像这样:
[[OEPocketsphinxController alloc] initWith:YES error:nil]
有点……
您正试图调用一个不存在的构造函数。相反,我们必须通过 sharedInstance
:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
sharedInstance()
是 OEPocketsphinxController
class 的 class 方法,其中 returns OEPocketsphinxController
.[=22= 的一个实例]
setActive(:error:)
是 OEPocketsphinxController
class 的实例方法,必须在此 class.
的实例上调用
所以,我们想使用 sharedInstance()
来获取一个实例,在该实例上调用 setActive(:error:)
方法。
下面两段代码完全等价:
Swift:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
Objective-C:
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];
我目前正在尝试将 objective-c 代码转换为 openEars 提供的示例应用程序的 swift。但是只有这一行代码:
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];
这个swift是怎么写的?
在框架中是这样定义的:
+ (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 Compiler Error Expected declaration
您调用的 Swift 代码在 Objective-C 中看起来像这样:
[[OEPocketsphinxController alloc] initWith:YES error:nil]
有点……
您正试图调用一个不存在的构造函数。相反,我们必须通过 sharedInstance
:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
sharedInstance()
是 OEPocketsphinxController
class 的 class 方法,其中 returns OEPocketsphinxController
.[=22= 的一个实例]
setActive(:error:)
是 OEPocketsphinxController
class 的实例方法,必须在此 class.
所以,我们想使用 sharedInstance()
来获取一个实例,在该实例上调用 setActive(:error:)
方法。
下面两段代码完全等价:
Swift:
OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
Objective-C:
[[OEPocketsphinxController sharedInstance] setActive:TRUE error:nil];