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

Bridging Swift class to React Native after Swift 4.2 upgrade

$
0
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?


Viewing all articles
Browse latest Browse all 16552

Trending Articles



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