I'm currently trying to convert the objective-c code to swift of the sample app provided by 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
The Swift code you've called would look like this in Objective-C:
Sort of...
You're trying to call a constructor which does not exist. Instead, we must go through the
sharedInstance:sharedInstance()is a class method of theOEPocketsphinxControllerclass which returns an instance of theOEPocketsphinxController.setActive(:error:)is an instance method of theOEPocketsphinxControllerclass and must be called on an instance of this class.So, we want to use
sharedInstance()to get an instance on which to call thesetActive(:error:)method on.The following two pieces of code are exactly equivalent:
Swift:
Objective-C: