Swift Compiler Compatibility Error

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.

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.

iOS Developer