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

前提是你 提交于 2019-12-04 10:13:12

问题


Short story: Xcode 10 is set to Swift 4, but it is using Swift 4.2.

Long story: Our project is not ready for Swift 4.2, since our dependent frameworks are not Swift 4.2, yet. But, I would like to, at least, use Xcode 10.

  • I have Xcode 9.4.1 and Xcode 10 installed.
  • I have Command Line Tools set to use Xcode 9.4.1:

  • In Xcode 10, I have the project setting, Swift Language Version, set to Swift 4.

  • I have verified on the command line the Swift version the shell is using is 4.1.2:

"swift --version Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)"

  • I've run carthage update.
  • I've quit Xcode 10 and relaunched.
  • I have deep cleaned and deleted Derived Data.
  • I build and I get this error:

"Module compiled with Swift 4.1.2 cannot be imported by the Swift 4.2 compiler: .../App/Carthage/Build/iOS/RealmSwift.framework/Modules/RealmSwift.swiftmodule/arm64.swiftmodule"

To me, this indicates that I have my intended Swift version (4.1.2) set correctly and Xcode is still trying to use the wrong version of Swift (4.2).

Does anyone have suggestions on how to resolve this? Am I missing something? Xcode bug?


回答1:


I'm not positive, but my guess is that this is the reason:

I have Command Line Tools set to use Xcode 9.4.1

You have to recompile your Swift 4 dependencies using Xcode 10, because Swift is not ABI-stable, i.e. binaries compiled with different compiler versions are not compatible. And even Swift 4.0 binaries compiled with different versions of Xcode may not be compatible.

I don't have much experience with Carthage, but my guess is that you should use Xcode 10 as a command-line tool, but you have to set a different Swift language version to be used by Xcode 10. It should probably be specified in the project (in this case RealmSwift), or using environment variables, but you'll need someone else to answer with more details, I'm afraid.




回答2:


Using Realm with Xcode 10 requires building it from source. In Terminal write:

git clone --recursive https://github.com/realm/realm-cocoa.git

cd realm-cocoa

sh build.sh ios-swift



回答3:


you can use multiple swift toolchains. download url swift toolchain download after you install some version. restart your xcode, in xocde menu you can see toolchains option.




回答4:


I had the same problem and then I realised I had not selected Xcode 10 as my Xcode. Try:

sudo xcode-select -s /Applications/Xcode10.app

Also, if you will be switching between Xcode9 and 10, I recommend you the following: If you change from 9 to 10, run mv Carthage Carthage9 and then rebuild Carthage. Whenever you want to switch back, move Carthage to Carthage10 and do mv Carthage9 Carthage. This will save you lots of time building.



来源:https://stackoverflow.com/questions/52413437/module-compiled-with-swift-4-1-2-cannot-be-imported-by-the-swift-4-2-compiler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!