Junior — Middle
Is it possible to connect and use Objective-C code through Swift Package Manager?
sobes.tech AI
Answer from AI
Swift Package Manager (SPM) is primarily designed for Swift and C/C++ code. Support for Objective-C in SPM is limited and not direct, as SPM cannot automatically create bridging headers for Objective-C.
However, you can use Objective-C code in a Swift Package if:
- The package contains Objective-C source files (.m and .h files).
- The Package.swift file specifies the appropriate settings to include Objective-C files.
- The Swift code uses modular imports, and the Objective-C files are properly exported through a module.
Example of a minimal Package.swift configuration for Objective-C:
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "MyPackage",
targets: [
.target(
name: "MyPackage",
dependencies: [],
path: "Sources",
publicHeadersPath: "include"
)
]
)
In the Sources/MyPackage folder, there should be Objective-C files and headers in the include folder.
Thus, it is possible to connect and use Objective-C code via Swift Package Manager, but with limitations and additional configuration. For more complex projects, CocoaPods or Carthage are often used, which better support Objective-C.