Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got "is not a recognized Objective-C method" when bridging Swift to React-Native

I'm trying to bridge my React-Native 0.33 code to a super simple Swift method, following this guide but all I'm getting is show:(NSString *)name is not a recognized Objective-C method.

Here's my code:

SwitchManager.swift

import Foundation

@objc(SwitchManager)
class SwitchManager: NSObject {

  @objc func show(name: String) -> Void {
    NSLog("%@", name);
  }

}

SwitchManagerBridge.h

#import "RCTBridgeModule.h"

@interface RCT_EXTERN_MODULE(SwitchManager, NSObject)

RCT_EXTERN_METHOD(show:(NSString *)name)

@end

SwitchManager-Bridging-Header.h

#import "RCTBridgeModule.h"

Then on my index.ios.js file I'm importing SwitchManager with import { SwitchManager } from 'NativeModules'; and calling SwitchManager.show('One');. This is where the error happened.

Not sure what's wrong.

like image 868
Benjamin Netter Avatar asked Sep 25 '16 21:09

Benjamin Netter


4 Answers

This is a part of Swift 3's changes and can be solved by adding an underscore:

import Foundation

@objc(SwitchManager)
class SwitchManager: NSObject {

  @objc func show(_ name: String) {
    NSLog("%@", name);
  }

}

See Swift 3's 0046 Proposal: Establish consistent label behavior across all parameters including first labels that is called out in the Swift.org migration guide under "Consistent first argument labels".

Basically, how Objective-C sees Swift methods has changed with Swift 3.

EDIT: This is still the case in Swift 4, see docs here under Omitting Argument Labels.

like image 195
James Wang Avatar answered Oct 17 '22 01:10

James Wang


this worked for me in xcode 8.0 and swift 3

@objc func openPresentedViewController(_ name: String,name1: String,name2: String){
}

add _ to non labelled members

RCT_EXTERN_METHOD(methodName:(NSString *)name name1:(NSString *)name1 name2:(NSString *)name2)

as you can see in the objective c method name is nonlabeled parameter add _ to it in the swift method

like image 24
Vijesh Krishna Avatar answered Oct 17 '22 01:10

Vijesh Krishna


I was fighting with this issue all day. Resolved by setting the Swift Compiler to use Legacy versions (XCode 8 is prefers Swift 3), so in:

Build Settings > Scroll down to 'Use Legacy Swift Language Version' set as Yes.

like image 3
designbuildplay Avatar answered Oct 17 '22 03:10

designbuildplay


I had the same error because I had forgotten to put the decorator @objc before the function declaration

like image 1
Raphael Pinel Avatar answered Oct 17 '22 02:10

Raphael Pinel