[英]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.