SwiftUI预览:无法在此文件中预览/启动失败

huangapple go评论73阅读模式
英文:

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[&quot;XCODE_RUNNING_FOR_PREVIEWS&quot;] 的检查,以确保与预览无关的服务未初始化,但每个视图预览都生成相同的错误消息,并附带相同无效的诊断报告。

英文:

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[&quot;XCODE_RUNNING_FOR_PREVIEWS&quot;] 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: &quot;...&quot;).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.

huangapple
  • 本文由 发表于 2023年5月28日 07:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定