Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

Unable to call swift function from objective-c in react-native

$
0
0

I'm fairly new to objective-c and swift so forgive me if this sounds dumb. Basically what I'm trying to do is *expose** a swift function to both react-native(so it can be used in JS) and to be used in objective-c. The issue I'm having is this dreaded "Duplicate interface definition for class...". I have researched and tried everything it seems but can not get rid of this error. I'm starting to wonder if is possible to do this. It seems so simple yet I just can't figure it out!

Here is my code:

AppDelegate.m

#import "MyApp-Swift.h"#import "MyApp-Bridging-Header.h"

MyApp-Swift.h

#import "React/RCTEventEmitter.h"@interface Counter : RCTEventEmitter- (void)start;- (void)stop;@end

MyApp.swift

import Foundationimport UIKit@objc(Counter)class Counter: RCTEventEmitter {  @objc func start(){  }  @objc func end(){  }}

MyApp-Bridging-Header.h

#import <Foundation/Foundation.h>#import "React/RCTBridgeModule.h"#import "React/RCTEventEmitter.h"@interface RCT_EXTERN_MODULE(Counter, RCTEventEmitter)RCT_EXTERN_METHOD(start);RCT_EXTERN_METHOD(end);@end

Inside AppDelegate.m didFinishLaunchingWithOptions() function

Counter* CounterInstance = [[Counter alloc] init];[CounterInstance start];

If I remove the code from MyApp-Swift.h then I get the error "No visible @interface for..." BUT fixes the "Duplicate interface error" in MyApp-Bridging-Header.h. It seems like it contradicts each other!? How are you supposed to call a swift function from objective-c while also exposing the same function to JS?


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>