Swift Compiler Compatibility Error

Anand Elumalai
2 min readApr 18, 2020

--

Module compiled with Swift 4.2 cannot be imported by the Swift 5.1.3 compiler

At some point of time, we would have come across above error statement in Swift. May be with different swift version numbers.

It means, we cannot use a Swift framework compiled in one version in a Swift project compiled in another version.

We will see how and why this happens and solution for it shortly.

How it happens?

This error can easily occur with the following setup.

  1. Create Swift framework (let’s say ShapeMaker.framework) in Xcode 10.0, which has Swift version 4.2
  2. Create Single View app (let’s say Shapes.xcodeproj) in Xcode 11.3.1, which has Swift version 5.1.3
  3. Add ShapeMaker.framework to Shapes.xcodeproj
  4. Add “import ShapeMaker” in any Swift file in Shapes.xcodeproj

Once this setup is compiled, error will be thrown.

Why it happens?

In order to understand it, we will take a look into framework structure

When Swift framework is included into a project and compiled, the Swift compiler imports that module using swiftmodule file. It is a compiled module having details of Public APIs available in the framework. More importantly, it is binary file containing internal compiler data structures.
Due to this, swiftmodule file differs with every version of swift compiler. So, if the module is used in one version of swift but created in another version, their compiler cannot understand it and this error is reported.

How to Solve it?

Binary Frameworks in Swift is the solution to this problem.

Swift libraries can be shipped in binary form using Binary frameworks. XCFrameworks is new supported way to distribute Binary frameworks, introduced in Xcode 11.

In case of XCFrameworks, the swiftmodule is replaced by swiftinterface file which is a plain text file, which future versions of swift can understand.

Thanks for reading! Hope you enjoyed this article.

If you’ve any questions or suggestions, please leave your response in comments section.

Interested to connect, can reach me at LinkedIn. Tweet at @AnandFnc to say Hello.

--

--