2

There is a swift project in which I have an objective-C file. I would like to call a Swift class function from Objective-C file

What is the best way to do this ? I have done it using NSNotification but wanted to know if there is a better way to do this.

Test-Bridging-Header.h

#import "ObjectiveC.h"

ClassA.swift

@objc class ClassA {

    func f1() {

    }
}

ObjectiveC.h

void objC_f1()

ObjectiveC.m

void objC_f1() {

    //Create an instance of ClassA and call function f1
}

Conclusion

  • There was some trouble caused because I was had a testcase scheme.
  • When I created a new project without a testcase scheme, it worked ok.
  • Pls Note - The -Swift.h generated but will not be listed in the project navigator
  • This answers my question - How to call Objective-C code from Swift
Community
  • 1
  • 1
user1046037
  • 16,755
  • 12
  • 92
  • 138
  • possible duplicate of [How to call Objective C code from Swift](http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift) – Mike D Apr 14 '15 at 14:16
  • It appears your example is not real code? The file names do not match the contents. Specifically, what is "Test-Bridging-Header.h". Is this a file you created manually? What are you importing at the top of ObjectiveC.h and .m? – Maury Markowitz Apr 14 '15 at 14:11
  • @MikeD that is just opposite of this question. – swiftBoy Jan 05 '16 at 04:53

1 Answers1

0

From Apple doc Apple Doc Link:

Importing Swift into Objective-C

When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h"

For example if your product name is MyApplication, you just have to import the generated file MyApplication-Swift.h.

bsarr007
  • 1,914
  • 14
  • 14
  • 1
    This is a swift project and as stated in the question a bridge file is added. Yet it doesn't work. – user1046037 Apr 14 '15 at 10:11
  • If i understand you question, you want to call swift functions from Objective-C no? – bsarr007 Apr 14 '15 at 10:13
  • Yes but this is a swift project. Is there a way to create an objective C bridge. If so could you post sample code. – user1046037 Apr 14 '15 at 10:14
  • You use the the bridge file to call Objective-C code from swift code but my response above show you how to make available Swift code inside Objective-C code. A `MyApplication-Swift.h` file is automatically generated(the name depend on your project/module name) created and you can use it. – bsarr007 Apr 14 '15 at 12:11
  • -Swift.h is not getting generated. How do I generate it ? Instead only Test-Bridging-Header.h was generated. But in build settings I am able to see the entry Test-Swift.h under "Objective-C Generated Interface Header Name" – user1046037 Apr 14 '15 at 12:48
  • What's the name of your application? – bsarr007 Apr 14 '15 at 15:54
  • I just realised that Test-Swift.h was getting generated but it was not listed in the list of files in the left panel (file browser panel) – user1046037 Apr 15 '15 at 01:01
  • Yes, it's not listed! – bsarr007 Apr 15 '15 at 08:31