我收到两个有关 SwiftUI 视图文件并发性的(重复)警告。当我在代码库的其他部分添加 Swift Concurrency 后,这种情况就开始发生了。
完整的警告是: SwiftUI.PreviewProvider Main actor-isolated static property '_previews'不能用于满足此处声明的非隔离协议要求'_previews'
警告是由下面的 CloseButton_Previews 引起的。这是完整的文件:
import SwiftUI
import DesignSystem
struct CloseButton: View {
private enum Constants {
static let closeIconSize = CGSize(width: 32, height: 32)
}
var body: some View {
HStack {
IconButton {
print("Close button was tapped")
} icon: {
Icon.closeBig
.resizable()
.symbolRenderingMode(.hierarchical)
.foregroundColor(.gray)
.frame(
width: Constants.closeIconSize.width,
height: Constants.closeIconSize.height,
alignment: .center
)
}
}
.frame(
width: Spacing.minimumTappableLength,
height: Spacing.minimumTappableLength,
alignment: .center
)
}
}
struct CloseButton_Previews: PreviewProvider …Run Code Online (Sandbox Code Playgroud) 我的 SwiftUI 预览将一个视图显示为 3 个不同的预览屏幕,它应该是一个具有组合视图的屏幕......
我究竟做错了什么?
谢谢你!
struct SignupSelfie: View {
@ObservedObject var signupImagesViewModel = SignupNavigationViewModel()
@State private var isValid = false
var body: some View {
VStack {
Button("capture") { /* action code here */ }
.background(
NavigationLink("", destination: SignupIdView(), isActive: $isValid)
)
}.navigationBarBackButtonHidden(true)
Spacer()
Text("capture selfie")
}
}
struct SignupSelfie_Previews: PreviewProvider {
static var previews: some View {
SignupSelfie()
}
}
Run Code Online (Sandbox Code Playgroud) 更新 Xcode 13 下面的代码示例在 Xcode 13 中按预期工作。
不幸的是,当前没有解决方法可以让您在实时预览之外预览此内容。
是否可以在不运行实时预览的情况下创建所呈现工作表的 SwiftUI 预览?例如:
struct Sheet_Previews: PreviewProvider {
static var previews: some View {
Text("Background").sheet(isPresented: .constant(true)) {
Text("Sheet")
}
}
}
Run Code Online (Sandbox Code Playgroud)
上述结果产生以下预览:
为了在预览中显示工作表内容,您必须运行实时预览:
我的项目由一个主要的 iOS 应用程序项目和三个用于表示层、域层和数据层的附加框架组成。我自己的框架嵌入在主要目标中。第三方框架由 CocoaPods 管理并动态链接。
现在,如果我尝试预览我的代码,则会收到以下错误:
RemoteHumanReadableError: Failed to update preview.
Error encountered when sending 'previewInstances' message to agent.
==================================
| RemoteHumanReadableError
|
| LoadingError: failed to load library at path "/Users/lucas/Library/Developer/Xcode/DerivedData/Envim-grqjeyyonytbnxcujdcadldmkoni/Build/Intermediates.noindex/Previews/Envim (iOS)/Products/Debug-iphonesimulator/Presentation.framework/Presentation": Optional(dlopen(/Users/lucas/Library/Developer/Xcode/DerivedData/Envim-grqjeyyonytbnxcujdcadldmkoni/Build/Intermediates.noindex/Previews/Envim (iOS)/Products/Debug-iphonesimulator/Presentation.framework/Presentation, 0): Library not loaded: @rpath/Resolver.framework/Resolver
| Referenced from: /Users/lucas/Library/Developer/Xcode/DerivedData/Envim-grqjeyyonytbnxcujdcadldmkoni/Build/Intermediates.noindex/Previews/Envim (iOS)/Products/Debug-iphonesimulator/Presentation.framework/Presentation
| Reason: image not found)
Run Code Online (Sandbox Code Playgroud)
我希望我包含了所有相关信息。如果我忘记了什么,请随时索取。
我的项目有 3 种不同的配置(本地、开发、发布),但无法让 SwiftUI 预览正常工作。
Compiling failed: no such module 'MyProject'...
Objects-normal/x86_64/MyView.2.preview-thunk.swift:1:64: error: no such module 'MyProject'
@_private(sourceFile: "MyView") import MyProject
Run Code Online (Sandbox Code Playgroud)
据我了解,问题在于模块名称,其中预览画布试图解析主版本名称“MyProject”,但在调试中它应该是 MyProject_Local (至少在 下设置该值Build Settings => Packaging => Product Module Name)。
我可能可以通过将调试产品模块名称设置为“MyProject”来破解此问题,但我想知道是否可以强制预览画布以某种方式使用正确的配置?
在我使用公共 Xcode 15.0 的旧代码库中,我无法使用新的#Preview宏预览任何 UIKit 视图
import SwiftUI
#Preview {
let uiView = UIView()
return uiView
}
Run Code Online (Sandbox Code Playgroud)
画布无法加载预览并进行故障诊断
Compiling failed: return expression of type 'UIView' does not conform to 'View'