框架内SwiftUI预览异常求助(Rosetta模式下)
问题解决:Rosetta模式下SwiftUI框架预览架构不兼容错误
问题背景
在模块化应用的UI.xcodeproject框架中运行SwiftUIView预览时触发错误,但主目标内的ContentView、GradientView预览正常:
- 涉及Xcode版本:14.0/14.1
- 触发条件:开启Rosetta模式(因第三方依赖必须保留)
- 已尝试无效操作:重建项目、清理Derived Data、修改
Build for Active Architecture选项 - 错误核心:预览代理尝试加载x86_64架构的
SwiftUIView.1.preview-thunk.dylib,但实际需要arm64架构
错误日志
PotentialCrashError: Update failed
XCPreviewAgent may have crashed. Check ~/Library/Logs/DiagnosticReports for any crash logs from your application.
==================================
RemoteHumanReadableError LoadingError: failed to load library at path "/Users/yevgeniy/Library/Developer/Xcode/DerivedData/Hackathon-abjguuvpzyisdncwvpeguazdifiu/Build/Intermediates.noindex/Previews/Hackathon/Intermediates.noindex/UI.build/Debug-iphonesimulator/UI.build/Objects-normal/x86_64/SwiftUIView.1.preview-thunk.dylib": Optional(dlopen(/Users/yevgeniy/Library/Developer/Xcode/DerivedData/Hackathon-abjguuvpzyisdncwvpeguazdifiu/Build/Intermediates.noindex/Previews/Hackathon/Intermediates.noindex/UI.build/Debug-iphonesimulator/UI.build/Objects-normal/x86_64/SwiftUIView.1.preview-thunk.dylib, 0x0002): tried: '/Users/yevgeniy/Library/Developer/Xcode/DerivedData/Hackathon-abjguuvpzyisdncwvpeguazdifiu/Build/Intermediates.noindex/Previews/Hackathon/Products/Debug-iphonesimulator/SwiftUIView.1.preview-thunk.dylib' (errno=2), '/Applications/Xcode_14.0.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Users/yevgeniy/Library/Developer/Xcode/DerivedData/Hackathon-abjguuvpzyisdncwvpeguazdifiu/Build/Intermediates.noindex/Previews/Hackathon/Intermediates.noindex/UI.build/Debug-iphonesimulator/UI.build/Objects-normal/x86_64/SwiftUIView.1.preview-thunk.dylib' (errno=2), '/Users/yevgeniy/Library/Developer/Xcode/DerivedData/Hackathon-abjguuvpzyisdncwvpeguazdifiu/Build/Intermediates.noindex/Previews/Hackathon/Intermediates.noindex/UI.build/Debug-iphonesimulator/UI.build/Objects-normal/x86_64/SwiftUIView.1.preview-thunk.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))) |
==================================
解决方案
方案1:调整UI框架的排除架构设置
- 选中
UI.xcodeproject框架目标,进入Build Settings - 搜索
Excluded Architectures,展开Debug-iphonesimulator配置 - 添加
x86_64到排除列表,强制框架为arm64架构编译预览产物 - 清理Derived Data后重新尝试预览
方案2:显式设置框架的有效架构
- 在Build Settings中,点击左上角
+号,选择Add User-Defined Setting - 创建
VALID_ARCHS设置,针对Debug-iphonesimulator配置,填入arm64 - 确保
Build Active Architecture Only在Debug配置下设置为YES - 重启Xcode并重新构建预览
方案3:修改框架Scheme的运行架构
- 打开UI框架的Scheme(
Product > Scheme > Edit Scheme) - 切换到Run标签页的Info选项卡
- 将
Architecture设置为arm64,而非Standard Architectures - 保存Scheme后重新尝试预览
内容的提问来源于stack exchange,提问作者Eugene P
相关产品推荐
相关产品推荐

