Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Objective C framework for swift app, error: Argument passed to call that takes no arguments

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

at the line I am calling the function.
Any thoughts?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading