0

I'm trying to call an Objective-C class from Swift. So I enabled the bridging header option

Created the following files:

MyTest.h:

#import <Foundation/Foundation.h>

@interface MyTest : NSObject
+(void)sayHello;
@end

MyTest.m

@implementation MyTest

+(void)sayHello {
    NSLog(@"Hello");
}

@end

at MyProject-Bridging-Header.hadded:

#import "MyTest.h"

Then when in a Swift file I call:

let test = MyTest()
test.sayHello()

The compiler complains in the second line:

MyTest does not have a member named 'sayHello'

What am I doing wrong?

Note: I have no experience with Objective-C.

Chirag Kothiya
  • 955
  • 5
  • 12
  • 28
Addev
  • 31,819
  • 51
  • 183
  • 302

1 Answers1

0

You created a class method, not an instance method.

Try instead:

MyTest.sayHello()

Or change the objective-c function to:

-(void)sayHello

Note the - instead of the +. See this question and answers for further reading.

Community
  • 1
  • 1
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222