Objective-C calling into Swift in Turbo Module library #783
Replies: 3 comments 3 replies
-
|
I figured out what I was missing here. Firstly, the Swift file needs to be a public class and have public functions
import Foundation
- class SwiftImpl : NSObject {
+ @objc
+ public class SwiftImpl : NSObject {
@objc
- func multiply(a: Double, b: Double) -> NSNumber {
+ public func multiply(a: Double, b: Double) -> NSNumber {
return NSNumber(value: a * b)
}
}The bridging header file isn't required in this case but it doesn't seem to hurt including it. The - s.source_files = "ios/**/*.{h,m,mm,cpp}"
+ s.source_files = "ios/**/*.{m,mm,cpp,swift}"Note the removal of the Which comes from this issue: CocoaPods/CocoaPods#12105 (comment) |
Beta Was this translation helpful? Give feedback.
-
|
where you able to add swift support for turbomodules? |
Beta Was this translation helpful? Give feedback.
-
|
Yeah that's one part,
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have created a library template using:
npx create-react-native-library objc-swift-example, running version0.48.6ofcreate-react-native-libraryand naming the packagereact-native-objc-swift-example.I've selected
Turbo Moduleand thenKotlin & Objective-C. Then I run the following commandsThis successfully builds and shows a screen saying 'Result: 21'. At this point the implementation of the
multiplyfunction is in Objective-C++ios/ObjcSwiftExample.hios/ObjcSwiftExample.mmI've then tried to implement the
multiplyfunction in Swift that is currently implemented byios/ObjcSwiftExample.mm. To do this I've createdios/SwiftImpl.swiftios/ObjcSwiftExample-Bridging-Header.h(an empty file)And then modified
ios/ObjcSwiftExample.mmRunning
pod install && yarn iosreturns the errorerror 'ObjcSwiftExample-Swift.h' file not found.I then tried modifying the file
ObjcSwiftExample.podspecto includeswiftsource filesRunning
pod install && yarn iosthen returns the errorerror 'generated/RNObjcSwiftExampleSpec/RNObjcSwiftExampleSpec.h' file not found. I believe this is due to the code in the podspec fileBut I think this was required to solve another issue: #690.
If I comment this code out then I run into another error, so I don't think commenting out this code is a path I want to go down.
Reverting that commenting out change, I have also tried adding other options into the podspec file such as
DEFINES_MODULES = YES, adding into theHEADER_SEARCH_PATHSthe path for theios/generateddirectory. I am unsure if I should use theSWIFT_OJBC_BRIDGING_HEADERalso as Xcode seems to populate this when creating a sample Objective-C project and adding a Swift file into it, but I've not had joy with that either.Is anyone able to help with what I am missing here?
Beta Was this translation helpful? Give feedback.
All reactions