- Create a project, select the language “Swift”.
- In the project “Build Settings”, “Other Link Flags“, add a value “-ObjC”.
- Create a Objective C file, then you’ll be asked, “Would you like to configure an Objective-C bridging header?” Click “Yes”. This bridging header file gonna be named with “ProjectName-Bridging-Header.h“. Open this file, you’ll see the comment “Use this file to import your target’s public headers that you would like to expose to Swift.” Just do what it says.
Now you can access your Objective-C files from your Swift file. Want to use Swift in Objective-C? Just
- Import “ProjectName-Swift.h” in the .m file where you want to use Swift. System gonna automatically generate this Swift.h file for you. This file will not show in the project navigator. With some version of Xcode, you can shift+cmd+o to search and open this file, after you build/run the target successfully. But some other version of Xcode, you can’t.
Note: The ProjectName-Swift.h file cannot be able to imported in Objective-C .h file, and so that, Objective C class can not subclass Swift class.
If you have multiple Objective-C file using Swift, it’s better to import the ProjectName-Swift.h file in the pch file. If you don’t have a pch file yet, follow “New file ->iOS:Other->PCH file” to create one. Don’t forget to change the project settings, “Precompile Prefix Header” with value “Yes”, and add the file path for “Prefix Header”.