Using Objective-C Classes in Swift
If you are going to import code within an App Target (Mixing Swift and Objective-C in one project) you should use bridging header file to expose Objective-C code to Swift code. [Mixing Swift and Objective-C code in a project]
In this post I will describe how to import Objective-C framework to Swift code
Swift consumer -> Objective-C dynamic framework
Xcode version 10.2.1
Create Objective-C framework
Create a framework project or create a framework target
File -> New -> Project... -> Cocoa Touch Framework
//or
Project editor -> Add a Target -> Cocoa Touch Framework
Two files will be generated:
Info.plist - Build Settings -> Info.plist File
<product_name>.h - Build Phases -> Headers. It is umbrella header file which will be open for consumer[About]
Add all .h files to this umbrella file(<product_name>.h)
#import "header_1.h"
#import "header_2.h"
Add Implementation files .m
Select `.m` file -> Select File Inspectors Tab -> Target Membership -> Select the target
//or
Project editor -> select a target -> Build Phases -> Compile Sources -> add files
Add Headers files .h that were listed in <product_name>.h in public zone (header_1.h, header_2.h)[can not do it] [public target membership]
Select `.h` file -> Select File Inspectors Tab -> Target Membership -> Select the target and make it **public**
//or
Project editor -> select a target -> Build Phases -> Headers -> add files to the **public** zone
Build the framework - ⌘ Command + B or Product -> Build
Note: Be sure that you build the framework for the same process architecture as the client code.
Find generated output[Build location]
Products group -> <product_name>.framework -> Show in Finder
The framework includes
Info.plist
Modules folder with:
module.modulemap[About] [Custom modulemap] This file was autogenerated because Build Settings -> Defines Module -> YES
Headers folder with:
- files from
Headers section. There are public interfaces/definitions
Swift consumer with Objective-C framework
Drag and drop the binary into the Xcode project[About]
Embed binaries[Library not loaded] [Link vs Embed]
Project editor -> select a target -> General -> Embedded Binaries -> path to `<product_name>.framework` file
I will automatically add the framework to:
Project editor -> select a target -> General -> Linked Frameworks and Libraries
Project editor -> select a target -> Build Phases -> Embed Frameworks
Project editor -> select a target -> Build Phases -> Link Binary With Libraries
Add Framework Search paths(FRAMEWORK_SEARCH_PATHS)[Module not found] [Recursive path]
Project editor -> select a target -> Build Settings -> Search Paths -> Framework Search paths -> add path to the parent of `<product_name>.framework` file
Import module to the Swift client code[module_name]
import module_name
More examples here