I try to integrate Swift
code in my app.My app is written in Objective-C
and I added a Swift
class. I\'ve done everything described he
Don't create the header file yourself. Delete the one you created.
Make sure your Swift classes are tagged with @objc
or inherit from a class that derives (directly or indirectly) from NSObject
.
Xcode won't generate the file if you have any compiler errors in your project - make sure your project builds cleanly.
There is two condition,
So, For that purpose, you have to follow this steps:
Go to Build Settings and perform below steps with search,
After that, clean and rebuild your project.
In that case,First write "@objc" before your class in swift file.
After that ,In your objective c file, write this,
#import "YourProjectName-Swift.h"
In that case, In your header file, write this,
#import "YourObjective-c_FileName.h"
I hope this will help you.
Allow Xcode to do its work, do not add/create Swift header manually. Just add @objc before your Swift class ex.
@objc class YourSwiftClassName: UIViewController
In your project setting search for below flags and change it to YES (Both Project and Target)
Defines Module : YES
Always Embed Swift Standard Libraries : YES
Install Objective-C Compatibility Header : YES
Then clean the project and build once, after build succeed (it should probably) import below header file in your objective-c class .m file
#import "YourProjectName-Swift.h"
Boooom!
I had the same issue and it turned out special symbols in the module name are replaced by xcode (in my case dashes ended up being underscores). In project settings check "module name" to find the module name for your project. After that either use ModuleName-Swift.h
or rename the module in settings.
Using Swift Classes in Objective-C
If you are going to import code within an App Target (Mixing Objective-C and Swift in one project) you should use the next import line #import "<#YourProjectName#>-Swift.h"
to expose Swift code to Objective-C code [Mixing Swift and Objective-C code in a project]
In this post I will describe how to import Swift static library to Objective-C code
Xcode version 10.2.1
Follow Create Swift static library with next additions:
Expose Swift API. To use Swift's functions from Objective-C[About]
After building you should find a <product_name>-Swift.h
file that should be located into DerivedSources
[File not found]
Drag and drop
the binary into the Xcode project[About]
Link Library
[Undefined symbols] [Link vs Embed]
Project editor -> select a target -> General -> Linked Frameworks and Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file
//or
Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file
Add Library Search paths
[Library not found for] [Recursive path]
Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib<product_name>.a` file
Add Header Search Paths
[Module not found] [Recursive path]
Project editor -> select a target -> Build Settings -> Search Paths -> Header Search Paths -> add path to generated `<product_name>-Swift.h` file
Add empty .swift file
to the Objective-C project.[Undefined symbols] When Xcode ask press Create Bridging Header
(it will create module_name-Bridging-Header.h
) and setup a path to this file in
Project editor -> select a target -> Build Settings -> Swift Compiler - General -> Objective-C Bridging Header
Import module to the Objective-C client code[File not found] [module_name]
#import "module_name-Swift.h"
More examples here
The file is created automatically (talking about Xcode 6.3.2 here). But you won't see it, since it's in your Derived Data folder. After marking your swift class with @objc
, compile, then search for Swift.h
in your Derived Data folder. You should find the Swift header there.
I had the problem, that Xcode renamed my my-Project-Swift.h
to my_Project-Swift.h
Xcode doesn't like
"." "-"
etc. symbols. With the method above you can find the filename and import it to a Objective-C class.