Importing Swift file to Objective-C project

I think it is very complex when I read Apple's reference. Hence, I decided to write a simple article which illustrates you how to import Swift file into Objective-C project step by step.

First, you should create an Objective-C project.

After project be created. We have some parameters should be set in Build Settings.

  • Find “Defines Module”, and set to “YES”. This setting let Xcode use same framework to import Swift to Objective-C project.



  • Find “Always Embed Swift Standard Libraries”, and set to “YES”.



  • The “Product Module Name” names by your project name. In there, we do not need to revise anything.


  • New or add exist Swift file into the project. Then Xcode pop-ups a dialog, follows image. We just click “Create Bridging Header” to automatically crate bridging header. After that, we can see the bridging header file which be names your product name followed by adding "-Bridging-Header.h".


    Finally, we need to import “MixDemo-Swift.h” to any Object-C .m. That header is created by Xcode, but you cannot see in the project folder. The file names your Product Module Name, which we mentioned above, followed by add “-Swift.h”. You can also search "Objective-C Generated Interface Header Name" under Build Settings.
    #import "MixDemo-Swift.h"

    Then, we can use swift object in the Objective-C project directly.
        BarcodeScannerViewController *barcodeViewController = [[BarcodeScannerViewController alloc] init];
    barcodeViewController.delegate = self;
    [self presentViewController:barcodeViewController animated:YES completion:nil];

    Reference: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
    profile-image
    Hello, My name is Nick, Taiwanese🇹🇼. I like to develop iOS Apps and use Unix-based operating systems. And, I love reading and traveling to fill to my life. This blog recorded all technical articles which I have faced and solved problems. Please leave your comment to discuss with me.
    comments powered by Disqus