简体   繁体   中英

Calling Swift function from Objective-C in a swift project

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM