0

In Swift, my class is something looks like below

@objc public class DemoSDK: NSObject { public static var shared = DemoSDK() public func connect(delegate: DemoSDKDelegate?) { } } 

I call a method from the swift like below (I am trying to write an equivalent objective-c++ code of below line)

DemoSDK.shared.connect(delegate: self) 

How can I call swift file function from objective-c++ code? I already imported header and all related classes into objective-c++ class, also all the required class and methods are marked with @objc

(Note: class in swift do not have constructor or initializer)

DemoSDK *obj = [[DemoSDK alloc] init]; // Error 'init' is unavailable 
2
  • The posted code does not show @objc tag on 'shared' property or 'connect' function. And there is no '@objc public init()' Commented Nov 8, 2018 at 6:26
  • Facing the same issue rn, how did you "import" the Swift file in Objective-C++? Commented Feb 25, 2021 at 20:17

2 Answers 2

4

Add getInstance Method in swift file:

@objc public class DemoSDK: NSObject { private static var shared = DemoSDK() @objc public static func getInstance() -> DemoSDK { return shared } @objc public func connect(delegate: DemoSDKDelegate?) { } } 

obj-C code :

[[DemoSDK getInstance] connect: self]; 
Sign up to request clarification or add additional context in comments.

2 Comments

No known class method for selector 'getInstance' . No known instance method for selector 'connect:'
i have edited my response. can you please take a look again ?
1

Mark all public classes, protocols, initializers, functions and properties with @objc

2 Comments

That I already did, but how to call a method using a static instance of class?
@harshal shows this in his updated answer: [DemoSDK getInstance].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.