美文网首页iOS学习笔记真的是答案
加载Swift Framework报错:Module compi

加载Swift Framework报错:Module compi

作者: 韦弦Zhy | 来源:发表于2020-04-03 11:31 被阅读0次

    Module compiled with Swift 5.1 cannot be imported by the Swift 5.2 compiler

    Module compiled with Swift 5.2 cannot be imported in Swift 5.0

    ...
    最近在用Swift写SDK,编译后给别人使用,报了很多诸如此类的错误,现在汇总一下解决方法。

    问题产生的原因就是Swift编译的Framework的swift版本和使用者APP使用的Swift版本不一致就会报这个错误,所以解决思路是同步Swift版本:

    一、使用carthage管理的第三方库报此类错误
    carthage update --platform iOS --no-use-binaries
    
    二、使用cocoaPods管理的第三方库报此类错误
    1. 删除 \Pods 文件夹内容
    2. pod install
    
    三、从https://swift.org/download/#releases下载安装适用于您的特定Xcode版本的Xcode Toolchain。

    Xcode Toolchain包括编译器,lldb以及其他相关工具的副本,这些副本可提供在特定版本的Swift中工作时提供相应环境。
    打开Xcode的首选项,Components > Toolchains ,然后选择已安装的Swift工具链。现在,您可以编译并运行该应用程序。

    四、自己下载Framework库源码使用自己的Xcode编译。
    五、自己是Framework的作者,但是不想开源,所以不能交由一些第三方管理库编译,或者给源码给别人编译:
      1. 自己编译多套Framework,每种Swift版本编一个。。。。😂
      1. 配置Framework的Build Setting将“Build Libraries for Distribution”选项设置为“YES”,否则Swift编译器不会生成必要的.swiftinterface文件,这是将来编译器能够加载旧库的关键。
    BUILD_LIBRARY_FOR_DISTRIBUTION = YES;
    

    修改完之后 Product > Clean Build Folder 然后再次 Build

      1. 配置了如果还是报错,如果是类名冲突
        如果模块是使用BUILD_LIBRARIES_FOR_DISTRIBUTION构建的,并且包含与模块本身名称相同的公共类型,则客户端将无法导入该模块。
        (19481048)(FB5863238)
        解决方法:重命名类型或模块以消除冲突。
      1. 如果2,3,还是无法解决,确认你的Framework是否引用了其他OC库,同时使用了OC库定义的Block,不配置BUILD_LIBRARIES_FOR_DISTRIBUTION使用时完全OK的,配置了之后无法使用,这时候你的选择有三:
    • 创建一个OCFramwork使用OC包裹这个Frawork
    • 使用OC重写这个Swift框架
    • 或者看请看1 😂

    参考:

    Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2 compiler
    Module compiled with Swift 5.0.1 cannot be imported by the Swift 5.1 compiler
    Module compiled with swift 4.0 cannot be imported in swift 3.1
    Module compiled with Swift 4.0 cannot be imported in Swift 4.0.1
    https://bugs.swift.org/browse/SR-11704
    https://developer.apple.com/documentation/xcode_release_notes/xcode_11_2_release_notes
    WWDC 2019对于此问题的视频https://developer.apple.com/videos/play/wwdc2019/416/
    Swift Framework 转成 OC Framework 来做版本兼容

    相关文章

      网友评论

        本文标题:加载Swift Framework报错:Module compi

        本文链接:https://www.haomeiwen.com/subject/owocphtx.html