0

I created one swift project on Xcode 9 using Swift 4. And and some objective c .h and .m files in my swift project by creating.h bridge file.

I am able to call objective c function from swift class but now i want to call swift function from objective c .h and .m file.

But i am not able to call swift function from objective c function

So what are the steps about that?

Kamran
  • 14,987
  • 4
  • 33
  • 51
mab
  • 225
  • 3
  • 10
  • thank you everyone to gave answer my question. I got answer of this question as more clearly. please refers bellow given link http://pinkstone.co.uk/how-to-use-swift-classes-in-objective-c/ – mab Apr 29 '18 at 05:26

2 Answers2

0

Importing "<ProductName>-Swift.h" at the top of your objc file should work.

For example:

#import myCuteCat-Swift.h

Also, don't forget @objc to the Swift method/attribute you want to expose.

bubu
  • 333
  • 2
  • 9
  • Please can you explian me as details stepwise? Means which productfile? Can you give me example? – mab Apr 26 '18 at 18:09
0

In order to call a Swift function or property from Objective-C you have to mark it with the @objc keyword. Example:

@objc func myFunc()

jakehawken
  • 707
  • 7
  • 12
  • Where do i define @objc func in my project? – mab Apr 26 '18 at 18:10
  • It’s a keyword. You don’t need to define it. Prepend the function or property with it as I did in my example above. – jakehawken Apr 26 '18 at 18:16
  • means directly use my swift function into objective c class. is their no need to import anything in objective c? and how do compiler know if I have same name swift function in my project which function want to call? – mab Apr 27 '18 at 04:18
  • Nope. No imports needed. That’s what the bridging header is for. It does that for you. – jakehawken Apr 27 '18 at 04:36
  • Why don’t you go try it and see. Try first, ask second. – jakehawken Apr 27 '18 at 04:37
  • Yes I tried I mark @objc func myFunction name in my swift file and now I call it from my objective c file but it does not seem in my objective c file and does not jump to definition so asked you for details. Is I t write way or not? Now when I call from objective c its get me error "Implicit declaration of function 'my function' is invalid in C99" – mab Apr 27 '18 at 04:51
  • Well, it appears now that this question has been marked as a duplicate, so I'll bet that following the directions here will work great: https://stackoverflow.com/questions/24078043/call-swift-function-from-objective-c-class – jakehawken Apr 27 '18 at 23:30