I am building an objective C framework for a swift app. I am trying to use a function in swift by an objective C header file that is imported.
interface MyApi : NSObject
(void)init:(NSDictionary *)launchOptions;
@end
@implementation MyApi
(void)init:(NSDictionary *)launchOptions {
...
}
@end
These are the .h and .m files in obj C. In swift I am trying to call the function like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
MyApi.init(launchOptions)
return true
}
But I keep getting the error: Argument passed to call that takes no arguments
at the line I am calling the function. Any thoughts?
CodePudding user response:
This syntax:
MyApi.init(launchOptions)
calls the initialiser of MyApi
. Note that init
is a keyword in Swift and has special meaning! Since MyApi
inherits from NSObject
, it inherits the parameterless initialiser, which is what the Swift compiler thinks that you are trying to call. This is the same as:
MyApi(launchOptions)
However, your init
method in Objective-C is not an initialiser for a MyApi
object, is it? It's just a regular class method called init
. To call it in Swift, you have to escape the special meaning of the init
keyword by adding backticks around it:
MyApi.`init`(launchOptions)