从SPM包运行SwiftUI预览 [英] Run SwiftUI preview from SPM Package

查看:83
本文介绍了从SPM包运行SwiftUI预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Swift Package Manager软件包,其中需要包含SwiftUI视图.

I am developing a package of Swift Package Manager where SwiftUI views need to be included.

每个SwiftUI视图都应该有一个预览.

Every SwiftUI view should have a preview.

但是,当我尝试运行任何视图的预览时,出现以下错误:

However, when I try to run a preview of any view I get the following error:

我找到了一种方法,如果我将打包方案更改为主要目标方案,它就会起作用.但是使用这种方法,我必须一直在方案之间切换,并且每次我想运行软件包预览时都需要构建主要目标.

I found a way where if I change the package scheme to the main target scheme it becomes work. But using this way I have to switch between schemes all the time and the main target needs to be built every time I want to run a preview of a package.

是否可以直接从SPM包中以某种方式运行SwiftUI预览而无需构建主要目标?

Is it possible to run SwiftUI preview somehow directly from SPM Package without building the main target?

推荐答案

Xcode 12

使用Xcode 12,SwiftUI预览版可以在独立程序包中按需运行

With Xcode 12 the SwiftUI Preview just works 'as-should' in standalone Package

Xcode 11 +

是否可以直接从SPM以某种方式运行SwiftUI预览 打包而没有建立主要目标?

Is it possible to run SwiftUI preview somehow directly from SPM Package without building the main target?

否,至少到现在为止(Xcode 11.4beta3).预览是Simulator的一种变体,它需要UI可执行文件才能为您的视图预览设置全功能的运行时上下文.

No, at least till now (Xcode 11.4beta3). Preview is a variant of Simulator and it needs UI executable to setup full-functional run-time context for your view preview.

解决方案(来自实践):设置SwiftUI可执行目标,该目标并行包含程序包(或程序包本身,取决于)中的所有文件,并对其进行所有SwiftUI开发,但程序包本身是在持续集成过程中构建的(包括单元测试)

Solution (from practice): setup SwiftUI executable target that in parallel contains all files from package (or package itself, depends) and perform all SwiftUI development it it, but package itself build during continuous integration process (including unit-testing).

这篇关于从SPM包运行SwiftUI预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆