Objective-C calling into Swift in Turbo Module library #783
Unanswered
Zach-Dean-Attractions-io
asked this question in
Q&A
Replies: 1 comment 1 reply
-
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.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I have created a library template using:
npx create-react-native-library objc-swift-example
, running version0.48.6
ofcreate-react-native-library
and naming the packagereact-native-objc-swift-example
.I've selected
Turbo Module
and 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
multiply
function is in Objective-C++ios/ObjcSwiftExample.h
ios/ObjcSwiftExample.mm
I've then tried to implement the
multiply
function in Swift that is currently implemented byios/ObjcSwiftExample.mm
. To do this I've createdios/SwiftImpl.swift
ios/ObjcSwiftExample-Bridging-Header.h
(an empty file)And then modified
ios/ObjcSwiftExample.mm
Running
pod install && yarn ios
returns the errorerror 'ObjcSwiftExample-Swift.h' file not found
.I then tried modifying the file
ObjcSwiftExample.podspec
to includeswift
source filesRunning
pod install && yarn ios
then 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_PATHS
the path for theios/generated
directory. I am unsure if I should use theSWIFT_OJBC_BRIDGING_HEADER
also 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