0

In Swift 3.0, I have the following Swift class

import Foundation

@objc(NetworkManager)
class NetworkManager: NSObject {


  init(...) {
    ...
  }


  deinit {
    ...
  }

  func isConnected(resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
    ...
  }
}

And the following bridge:

#import <Foundation/Foundation.h>

#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(NetworkManager, NSObject)
  _RCT_EXTERN_REMAP_METHOD(isConnected, isConnectedWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject, NO)
@end

After upgrading to Swift 4.2, this begins to fail with isConnnectedWithResolver:rejecter is not a recognized Objective-C method.

After looking around and finding this question: Got "is not a recognized Objective-C method" when bridging Swift to React-Native, and this code sample: https://github.com/bigali/StringToHash/blob/master/ios/StringToHash/StringToHash.m

I've changed this to:

import Foundation

@objc(NetworkManager)
class NetworkManager: NSObject {


  init(...) {
    ...
  }


  deinit {
    ...
  }

  func isConnected(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
    ...
  }
}

and:

   #import <Foundation/Foundation.h>
   #import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(NetworkManager, NSObject)
  _RCT_EXTERN_REMAP_METHOD(isConnected, isConnected:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject, NO)
@end

Which only changes the function name in the error message to:

isConnnectedWithResolver:rejecter is not a recognized Objective-C method.

Whats causinng this problem? Why is the solution outlined in the other questionn not working? And how do I fix this problem?

Abraham P
  • 15,029
  • 13
  • 58
  • 126
  • Can you declare `RCTPromiseRejectBlock` to be @objc? – Alex Sep 03 '19 at 18:52
  • how would I do that? I can't get rid of the @escapingn as I do need the promise (this being a JS promise) to survive the call to isConnected? – Abraham P Sep 03 '19 at 21:14

1 Answers1

0

You shold import eventEmitter to. Put it into your header bridge file and in your objective-c files

#import "React/RCTEventEmitter.h"
hnrqss
  • 41
  • 8