简体   繁体   中英

Import ViewController.swift in Objective C file

I'm trying to use my new ViewController.swift file in my existing objective C project.

Below is swift file code

import UIKit
class TutorialViewController: UIViewController{
}

Below is Objective C code

#import "TutorialViewController-Swift.h"  //"TutorialViewController-Swift.h" file not found

I'm unable to import swift code. I had followed all the steps in this

Please let me know, where am I making mistake. Is it only applicable for NSObject class.

Thanks in advance

You can't import a swift class directly to Objective C class like that way. By default Xcode generates a swift bridging header for this purpose. You need to import that header. Normally that header file uses the following naming convention:

<#your module name #>-Swift.h

Or you can get the value from your target's build settings:

  • Choose your target
  • Go to Build Settings tab
  • Go to Swift Compiler - Code Generation category
  • Check the value of Objective-C Generated Interface Header Name
  • Import that header in your objective-c class to use all your swift classes

As per document, 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" .

By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header. Declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction , @IBOutlet , or @objc as well. If your app target is compiled with testing enabled, a unit test target can access any declaration with the internal modifier as if they were declared with the public modifier by prepending @testable to the product module import statement.

You don't need to do anything special to create the generated header file—just import it to use its contents in your Objective-C code. Note that the Swift interfaces in the generated header include references to all of the Objective-C types used in them. If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types before importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.

For more details follow this Importing Swift into Objective-C

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