Skip to content

Instantly share code, notes, and snippets.

@jaclync
Last active October 16, 2020 15:33
Show Gist options
  • Save jaclync/218f18684d03bdef8d007b996105a977 to your computer and use it in GitHub Desktop.
Save jaclync/218f18684d03bdef8d007b996105a977 to your computer and use it in GitHub Desktop.
Swift <--> Objective-C import

Importing Objective-C into Swift

To import a set of Objective-C files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header for the framework.

  • Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes”.
  • In your umbrella header file (FrameworkName.h), import every Objective-C header you want to expose to Swift. For example:
#import <XYZ/XYZCustomCell.h>
#import <XYZ/XYZCustomView.h>
#import <XYZ/XYZCustomViewController.h>

Swift will see every header you expose publicly in your umbrella header. The contents of the Objective-C files in that framework will be available in any Swift file within that framework target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

let myOtherCell = XYZCustomCell()
myOtherCell.subtitle = "Another custom cell"

Importing Swift into Objective-C

To import a set of Swift files in the same framework target as your Objective-C code, you don’t need to import anything into the umbrella header for the framework. Instead, import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use your Swift code from.

Because the generated header for a framework target is part of the framework’s public interface, only declarations marked with the public or open modifier appear in the generated header for a framework target.

Swift methods and properties that are marked with the internal modifier and declared within a class that inherits from an Objective-C class are accessible to the Objective-C runtime. However, they are not accessible at compile time and do not appear in the generated header for a framework target.

For more information on access-level modifiers, see Access Control in The Swift Programming Language (Swift 4.1).

  • Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes”.
  • Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>

The Swift files in your framework target will be visible in Objective-C .m files containing this import statement. For information on using Swift from Objective-C code, see Using Swift from Objective-C.

\ Import into Swift Import into Objective-C
Swift code No import statement #import <ProductName/ProductModuleName-Swift.h>
Objective-C code No import statement; Objective-C umbrella header required #import "Header.h"

Reference:

Could be useful if we don't want to expose Objc headers in umbrella header: https://github.com/danieleggert/mixed-swift-objc-framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment