简体   繁体   中英

How to lmport swift class into ViewController?

i used following code showing error that file not found.

#import <SwiftDemo/ViewController-swift.h>

even ViewController.swift file is present in project, help me to sort out problem.

I assume you are trying to use Swift code from Objective-C. Is your product name "ViewController"? If not, then you are not importing the correct header. You should use the Xcode generated header ProductModuleName-Swift.h (replacing ProductModuleName with your specific product name).

In simplest terms, the ProductModuleName is just the name of your application. Example: my app is Application so this header is Application-Swift.h

Also, this header will connect ALL of your Swift to Obj-C, it is not file specific.

Please refer to this immensely helpful documentation piece on Swift/Obj-C interoperability.

You should import a synthetic file generated by the compiler called "<YourProductName>-Swift.h" .

I think that this file is generated on the fly, but you can still inspect what it expands to by eg creating a dummy Objective-C file that contains just this:

extern int ____BEGIN_SWIFT_IMPORTS____; // dummy marker declaration
#import "<YourProductName>-Swift.h"

(you should of course substitute <YourProductName> )

and then by running the preprocessor on it (Product → Perform Action → Preprocess). All the output after the dummy marker declaration will come from the generated file. It's useful to see how Swift code marked with @objc gets translated to Objective-C declarations.

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