Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class extensions not working on modules #2

Closed
tmoran-stenoa opened this issue Dec 11, 2023 · 5 comments
Closed

Class extensions not working on modules #2

tmoran-stenoa opened this issue Dec 11, 2023 · 5 comments

Comments

@tmoran-stenoa
Copy link

Hey!

Thank you so much for making this package. I've found a small issue: methods declared in extensions are not picked up by React. For example:

// TestModule.swift

import Foundation
import React
import ReactBridge

@ReactModule
class TestModule: NSObject, RCTBridgeModule {
  @ReactMethod
  @objc func sayHello() {
     print("Hello, world!")
  }
}

extension TestModule {
  @ReactMethod
  @objc func sayHello2() {
     print("Hello, world!")
  }
}

In the above, React is able to detect sayHello, but not sayHello2, so only the 1st can be invoked by the JS.

If I manually declare the methods, then it works:

#import "React/RCTBridgeModule.h"

@interface RCT_EXTERN_MODULE(TestModule, NSObject)

RCT_EXTERN_METHOD(sayHello)
RCT_EXTERN_METHOD(sayHello2)

@end

Any idea what could be causing this?

@tmoran-stenoa
Copy link
Author

Update: weirdly enough, if I explicitly add the static method which is generated by @ReactMethod, then it works:

extension TestModule {
 // React detects this!
  @objc static func __rct_export__sayHello2() -> UnsafePointer<RCTMethodInfo>? {
    struct Static {
      static let jsName = strdup("sayHello2")
      static let objcName = strdup("sayHello2")
      static var methodInfo = RCTMethodInfo(jsName: jsName, objcName: objcName, isSync: false)
    }
    return withUnsafePointer(to: &Static.methodInfo) {
        $0
    }
  }  

  @objc func sayHello2() {
     print("Hello, world!")
  }
}

@ikhvorost
Copy link
Owner

Hi! Looks like it is the compiler issue because it does't generate objc exported methods for React Native in the extensions. I saw the similar problems with Xcode 15.0 betas and hope it will be fixed in new releases of Xcode.

@tmoran-stenoa
Copy link
Author

Hi! Looks like it is the compiler issue because it does't generate objc exported methods for React Native in the extensions. I saw the similar problems with Xcode 15.0 betas and hope it will be fixed in new releases of Xcode.

That explains a lot, thanks. Is there anywhere where we could report this bug?

@ikhvorost
Copy link
Owner

@tmoran-stenoa, the issue is fixed on Xcode 15.3 and you can declare your bridged methods within extensions.

@tmoran-stenoa
Copy link
Author

@tmoran-stenoa, the issue is fixed on Xcode 15.3 and you can declare your bridged methods within extensions.

Nice, thanks! I can't confirm this right now but will take your word for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants