Xcode 16.2中SwiftUI预览因架构不兼容无法使用求助
- 环境:Xcode 16.2,MacBook Pro M3 Pro
- 问题:现有项目引入DotLottie、swiftui-mail-view等SPM依赖后,SwiftUI预览功能失效;但新建项目安装相同依赖后预览正常
- 核心报错:
Build failed because DotLottie.swiftmodule is not built for arm64. Would you like to show all run destinations instead?
Ensure all targets are configured to build for standard architectures. If your project uses external dependencies, contact those vendors to provide updated copies built to support all architectures. You can control the visibility of architecture-specific run destinations in the Product > Destination menu.
== PREVIEW UPDATE ERROR:
SchemeBuildError: Failed to build the scheme “Tenki Weather”
could not find module 'DotLottie' for target 'x86_64-apple-ios-simulator'; found: arm64-apple-ios-simulator, at: /Users/me/Library/Developer/Xcode/DerivedData/projectname/Build/Intermediates.noindex/Previews/iphonesimulator/myapp/Products/Debug-iphonesimulator/DotLottie.swiftmodule - 已尝试无效操作:删除Derived Data、重新添加SPM依赖、将x86_64加入排除架构
- 额外异常:添加模拟器时默认出现Rosetta版本,怀疑项目或模拟器设置被篡改
1. 恢复项目架构默认配置
打开项目根节点(Project Navigator最顶部),依次检查全局和所有Target的Build Settings:
- 搜索
Architectures,设置为$(ARCHS_STANDARD),确保Excluded Architectures为空(不要手动添加x86_64) - 搜索
Build Active Architecture Only,Debug设为Yes,Release设为No - 切换到Info标签页,确认Configurations下所有环境的Architectures为
Standard Architectures (arm64)
2. 重置模拟器并移除Rosetta设备
- 关闭Xcode和模拟器,打开终端执行以下命令清除模拟器缓存:
rm -rf ~/Library/Developer/CoreSimulator/Devices rm -rf ~/Library/Developer/Xcode/iOS DeviceSupport - 重新打开模拟器,执行
Simulator > Reset Content and Settings...完成重置 - 打开Xcode的
Window > Devices and Simulators,删除所有带“Rosetta”标识的模拟器 - 添加新模拟器时,确保不勾选**“Use Rosetta for x86_64 emulation on Apple Silicon”**选项
3. 修复SPM依赖构建问题
- 进入项目的
Package Dependencies页面,更新DotLottie等依赖到最新版本 - 若更新无效,删除依赖后重新添加,优先选择Exact Version而非分支版本
- 执行
Cmd+Shift+K清理构建文件夹,再重新尝试SwiftUI预览
4. 重置Xcode项目工作区
- 关闭Xcode,删除项目目录下的
.xcworkspace文件,以及.xcodeproj内的xcuserdata和project.xcworkspace文件夹 - 重新打开
.xcodeproj,Xcode会自动重建工作区配置 - 重新添加SPM依赖并测试预览
内容的提问来源于stack exchange,提问作者Tomas

