-1

In my objc project I need to use a swift pod. For this purpose I need to add @objc in swift pod classes only then I am able to use the delegates and other properties in objective c classes. My question is : Is there any way to use swift pod into objective c project without manually adding @objc in swift pod classes? Thanks in advance!

swiftDev
  • 41
  • 1
  • 5

1 Answers1

0

To answer your question: No, you will need to add @objc somewhere, that's the only way Swift methods can be callable from Objective-C.

Since need a place where the Swift methods are made available to your ObjC code you have basically two options:

a) Depending on how extensive your use of that pod is, you could get away with writing a small Swift wrapper around the pod, where you make the necessary calls and expose your wrapper methods via @objc.

b) Alternatively, I would suggest to fork the pod, make the changes for interoperability and offer to upstream your changes in a PR.

To illustrate a), let's say your pod has this:

class PodClass {
    func podMethod() -> Int {
        // ...
        return 42
    }
}

to make this available to Objective-C, you could implement

class PodWrapper: NSObject {
    private let podc = PodClass()

    @objc func podMethod() -> Int {
        return podc.podMethod()
    }

}

and then in Objective-C do

#import "YOURAPPNAME-Swift.h"

-(void) foo {
    PodWrapper* p = [PodWrapper new];
    NSInteger x = [p podMethod];
}
Gereon
  • 17,258
  • 4
  • 42
  • 73
  • I have searched a lot about "@objc" and u r right we can not use it without placing "@objc". But can u please elaborate a bit about writing swift wrapper around pod OR Please provide me any link how to write such wrapper. Thanks a lot. – swiftDev May 12 '20 at 12:08
  • added an example – Gereon May 12 '20 at 13:02