You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS SwiftUI Preview在多模块UIKit项目中崩溃

UIKit多模块项目子模块SwiftUI预览崩溃解决方案

问题描述

基于UIKit的多模块项目中,主模块添加SwiftUI视图可正常预览,但子模块(ProjectC)中添加SwiftUI预览时崩溃,核心错误为架构不兼容:编译出的子模块框架是x86_64架构,但XCPreviewAgent需要arm64架构,导致加载失败。完整错误信息如下:

XCPreviewAgent may have crashed. Check ~/Library/Logs/DiagnosticReports for any crash logs from your application.
RemoteHumanReadableError
LoadingError: failed to load library at path "/Users/andrew.maher/Library/Developer/Xcode/DerivedData/Project-eppsdymsecoxglarlduaphfzpwiv/Build/Intermediates.noindex/Previews/QA/Products/Debug (QA)-iphonesimulator/ProjectC.framework/ProjectC": Optional(dlopen(/Users/andrew.maher/Library/Developer/Xcode/DerivedData/Project-eppsdymsecoxglarlduaphfzpwiv/Build/Intermediates.noindex/Previews/QA/Products/Debug (QA)-iphonesimulator/ProjectC.framework/ProjectC, 0x0000): tried: '/Users/andrew.maher/Library/Developer/Xcode/DerivedData/Project-eppsdymsecoxglarlduaphfzpwiv/Build/Intermediates.noindex/Previews/QA/Products/Debug (QA)-iphonesimulator/ProjectC.framework/ProjectC' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Users/andrew.maher/Library/Developer/Xcode/DerivedData/Project-eppsdymsecoxglarlduaphfzpwiv/Build/Intermediates.noindex/Previews/QA/Products/Debug (QA)-iphonesimulator/ProjectC.framework/ProjectC' (errno=2), '/Users/andrew.maher/Library/Developer/Xcode/DerivedData/Project-eppsdymsecoxglarlduaphfzpwiv/Build/Intermediates.noindex/Previews/QA/Products/Debug (QA)-iphonesimulator/ProjectC.framework/ProjectC' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/ProjectC.framework/ProjectC' (errno=2))

解决方案

1. 统一子模块架构编译设置

  • 选中出问题的子模块(ProjectC)Target,进入Build Settings
  • 搜索Architectures:
    • 将Architectures设为Standard Architectures (arm64, x86_64)
    • 若存在Valid Architectures选项,确保包含arm64和x86_64
  • 搜索Build Active Architecture Only,将Debug模式下的该选项改为NO,强制编译所有兼容架构

2. 匹配预览设备架构

  • 点击Xcode顶部预览设备选择器,确保选择的模拟器架构与编译输出一致:M系列Mac选arm64模拟器,Intel芯片选x86_64模拟器
  • M系列Mac用户可尝试开启Rosetta编译:File > Project Settings > Build System,勾选Use Rosetta for x86_64 builds

3. 清理缓存重新编译

  • 执行Product > Clean Build Folder(快捷键Cmd+Shift+K)
  • 删除项目对应的DerivedData目录:前往~/Library/Developer/Xcode/DerivedData,找到Project-eppsdymsecoxglarlduaphfzpwiv文件夹删除
  • 重启Xcode后重新编译并尝试预览

4. 检查子模块SwiftUI依赖

  • 确认子模块的Build Phases > Link Binary With Libraries中已添加SwiftUI框架
  • 检查预览代码中无硬编码的架构相关逻辑

内容的提问来源于stack exchange,提问作者Andrew Maher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:23:09