Accessing a swift function with Parameters, from Objective C

I am trying to call a swift function with a parameter, from Objective C.


I can call the function fine if the function has no parameters.

eg.

func testFunction () {

}



Whenever I try to include a parameter in the function though, I can no longer access it,

eg.

func testFunction2 (filename: NSString) {

}

I can no longer call this function from objective C file.



At the top of my objective C .m file I have added

#import "TestProject-Swift.h"


My technique for calling the swift function is,

SwiftFile *swiftFileTest = [[SwiftFile alloc] init];

swiftFile.testFunction //THIS WORKS FINE


When I try to call swiftFile.testFunction2 above however, it is not even recognised by Xcode.


Can anyone help me with where I am going wrong thanks?

Accepted Answer

You need to learn one of the basics of the Objective-C language, message sending notation -- aka method call.

For methods with no arguments:

[{target-object} {method-name}];

So, your method `testFunction` can be called as:

[swiftFileTest testFunction];

In the modern Objective-C, you can call such no-argument method with property-like notation, as you tried:

swiftFileTest.testFunction;

The two forms of method calls are exactly equivalent.


And for methods with single argument:

[{target-object} {method-name:} {argument}];

So, your method `testFunction2:` can be called as:

[swiftFileTest testFunction2: @"/Users/dev/Desktop/test.txt"];

In this case, in Objective-C, you cannot use property-like notation.


(In Swift, function and method are different. You better choose such technical terms carefully.)

Thank You OOPer for helping me out.

It is much appreciated.

Accessing a swift function with Parameters, from Objective C
 
 
Q