Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a value from a Swift function in Objective C

I am trying to get a value from a Swift function of a class I made. The Swift file is recognized and properly included in a Objective C project via the MyProdName-Swift.h header. I can instanciate it, but I am not able to get a String from a function.

Swift class:

@objc public class Test : NSObject {

    @objc func getTest () -> NSString{

        return "tested"
    }
}

Objective C class:

Test *test = [Test new];
NSString *testDummy = test.getTest();

In the last line I get "Called object type 'NSString *' is not a function or function pointer".
So obviously I am making some basic mistake, please point me to it.

like image 336
DontMind Avatar asked Dec 12 '25 13:12

DontMind


1 Answers

You're trying to call the method in Objective C using Swift method call syntax. You should be doing this:

NSString *testDummy = [test getTest];
like image 60
Mick MacCallum Avatar answered Dec 14 '25 04:12

Mick MacCallum