无法从其模块接口构建模块;它可能已经损坏,或者它可能在生产时触发了 Swift 编译器中的错误

Failed to build module from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced

像这样创建xcframework时

xcodebuild archive -project endiosOne-iOS.xcodeproj -scheme EOFoundation -destination="iOS" -archivePath /tmp/xcf/ios.xcarchive -derivedDataPath /tmp/iphoneos -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

xcodebuild archive -project endiosOne-iOS.xcodeproj -scheme EOFoundation -destination="iOS Simulator" -archivePath /tmp/xcf/iossimulator.xcarchive -derivedDataPath /tmp/iphoneos -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

xcodebuild -create-xcframework -framework /tmp/xcf/ios.xcarchive/Products/Library/Frameworks/EOFoundation.framework -framework /tmp/xcf/iossimulator.xcarchive/Products/Library/Frameworks/EOFoundation.framework -output /tmp/xcf/EOFoundation.xcframework

然后将框架添加到现有项目或新项目,我们在导入框架时遇到此错误。

Failed to build module 'EOFoundation' from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced

使用 xcode 12.1,也尝试使用 Xcode 11。尝试使用新的 UIKit 应用程序和新的 SwiftUI only 应用程序

我们得到以下 UIKit 错误 UIKit Errors

我无法修复 UIKit 中的 UIkit 错误:不幸的是,苹果没有给我们很多关于如何解决这个问题的信息。

我该如何解决这个问题,以便我使用在另一个项目中生成的 xcframework?

解决方案是删除扩展中的 public 方法 UITextView: UITextViewDelegate {

} 并将其简单地添加到 UITextView 的子类中,该子类是文件私有的。

我就此问题向 Apple 发布了反馈请求,他们提出了以下建议:

我们可以告诉您,有一个已知问题符合您的描述:如果您的模块名为“MyModuleName”并且它还声明或导入了一个 type 称为“MyModuleName”,文件中应该引用模块的名称将改为解析为类型,从而在您尝试导入它时导致错误。 Swift 开源项目的错误跟踪器在 https://bugs.swift.org/browse/SR-14195

的 public 文章中描述了此错误及其解决方法

果然,我从我的框架中导出的类型的名称与我从中导入它的模块完全相同。重命名类型解决了我的问题。

在我的例子中,当我更改 iOS 部署目标时,我所依赖的预编译 XCFramework 没有被重新编译。我重新编译了 XCFramework 并解决了这个问题。

我创建了一个小脚本,通过修复 swiftinterface 文件来解决这个问题。

  • 替换框架名称
  • 添加或删除平台(目前它生成 ios 设备和 ios 模拟器)

    #!/bin/bash
    
    FrameworkName="MyLibAndClassName"
    
    rm -rf build/
    
    xcodebuild archive -scheme "$FrameworkName" \
        -configuration Debug -destination 'generic/platform=iOS' \
        -archivePath "./build/$FrameworkName.framework-iphoneos.xcarchive" \
        SKIP_INSTALL=NO \
        BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
    
    xcodebuild archive -scheme "$FrameworkName" \
        -configuration Debug -destination 'generic/platform=iOS Simulator' \
        -archivePath "./build/$FrameworkName.framework-iphonesimulator.xcarchive" \
        SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
    
    # Fix https://bugs.swift.org/browse/SR-14195 (caused by https://bugs.swift.org/browse/SR-898)
    pattern="./build/$FrameworkName.framework-iphoneos.xcarchive/Products/Library/Frameworks/$FrameworkName.framework/Modules/$FrameworkName.swiftmodule/*.swiftinterface"
    grep -rli "$FrameworkName.$FrameworkName" $pattern \
         | xargs sed -i '' "s,$FrameworkName.$FrameworkName,$FrameworkName,g"
    # end fix
    
    xcodebuild -create-xcframework \
        -framework "./build/$FrameworkName.framework-iphoneos.xcarchive/Products/Library/Frameworks/$FrameworkName.framework" \
        -framework "./build/$FrameworkName.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/$FrameworkName.framework" \
        -output "./build/$FrameworkName.xcframework"
    
    # Wait for process completion and verify result
    pid=$!      
    wait $pid
    echo "Process with PID $pid has finished with Exit status: $?"
    [[ ! -d "./build/$FrameworkName.xcframework/" ]] && { 
        msg="[ERROR] expected ./build/$FrameworkName.xcframework/ to exist"; echo -e $msg
        exit 1
    }