If you press the "ProductModuleName-Swift.h"
on the Xcode source file editor, you can see how the Swift methods map to Objective-C.
In your case it will be
@interface MySwiftClass : NSObject - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER; - (void)sayHello; - (NSInteger)addXWithX:(NSInteger)x andY:(NSInteger)y; @end
which is called
MySwiftClass* getData = [[MySwiftClass alloc]init]; [getData sayHello]; NSInteger result = [getData addXWithX:5 andY:5];
Best Swift 3 Method Name May Be
func add(x: Int, y:Int) -> Int
because x
already an argument (external) to the name of the first parameter. You can also add the @objc()
attribute to @objc()
Swift definition to control the Objective-C name. For example, when
@objc(addX:andY:) func add(x: Int, y: Int) -> Int { return x+y }
it will be called from Objective-C as
NSInteger result = [getData addX:5 andY:5];
source share