英文:
SwiftUI Previews: Cannot preview in this file / failed to launch
问题
我的 macOS 项目将成功构建和运行,但在 Xcode 中预览加载失败,出现以下错误消息:
无法在此文件中预览<br>无法启动 com.example.AppName
诊断数据生成了一条通用错误消息:
FailedToLaunchAppError: 无法启动 com.example.AppName
RemoteHumanReadableError
ProcessError: FBApplicationLaunchTransaction 成功,但未为 com.example.AppName 启动任何进程:/tmp/AppName/Build/Intermediates.noindex/Previews/AppName/Products/Debug/AppName.app
在我的应用程序逻辑中,我已包括对 ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"]
的检查,以确保与预览无关的服务未初始化,但每个视图预览都生成相同的错误消息,并附带相同无效的诊断报告。
英文:
My macOS project will build and run successfully, but previews fail to load in Xcode with the error message:
Cannot preview in this file<br>Failed to launch com.example.AppName
The diagnostics data yields a generic error message:
FailedToLaunchAppError: Failed to launch com.example.AppName
RemoteHumanReadableError
ProcessError: FBApplicationLaunchTransaction succeeded with no process for com.example.AppName:/tmp/AppName/Build/Intermediates.noindex/Previews/AppName/Products/Debug/AppName.app
Within my application's logic, I've included checks against ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"]
to ensure that services unrelated to previews aren't initialized, but every View preview produces the same error message with the same unhelpful diagnostic report.
答案1
得分: 1
这个错误的原因是在我的项目配置的其他链接器标志部分包含了链接器标志-undefined dynamic_lookup
。
我的项目在运行时使用Bundle(path: "...").load()
来加载一个框架,因此以这种方式设置链接器标志允许它在不生成未定义符号错误的情况下构建。然而,似乎以这种方式设置会导致 SwiftUI 预览不工作。
为了解决这个问题,我只需删除链接器标志,并手动将我引用的框架链接为一个非嵌入式依赖项。项目仍然可以构建,而 SwiftUI 预览也再次工作。
英文:
The cause of this error turned out to be inclusion of the linker flag -undefined dynamic_lookup
in the Other Linker Flags section of my project's configuration.
My project uses Bundle(path: "...").load()
to load a framework at runtime, so setting the linker flag in this way allowed it to build without generating undefined symbol errors. However, it appears that setting things up in this way also kills SwiftUI previews.
To fix this, I simply removed the linker flag and manually linked the framework I was referencing as a non-embedded dependency. The project still builds, and SwiftUI previews are once again working.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论