[英]SwiftUI: Generic parameter 'Subject' could not be inferred
我使用SwiftUI构建了一个LoadingView
,用于在我从API获取远程数据时显示我的应用程序中的一些加载内容。 我在Xcode版本11.0 beta 5上。
这是LoadingView
:
struct LoadingView<Content>: View where Content: View {
@Binding var isShowing: Bool
var content: () -> Content
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .center) {
self.content()
.disabled(self.isShowing)
.blur(radius: self.isShowing ? 3 : 0)
VStack {
Text("Loading...")
ActivityIndicator(isAnimating: .constant(true), style: .large)
}
.frame(width: geometry.size.width / 2,
height: geometry.size.height / 5)
.background(Color.white)
.foregroundColor(Color.primary)
.cornerRadius(5)
.opacity(self.isShowing ? 1 : 0)
}
}
}
}
这是我的DataStore。 它被声明为ObservableObject
并具有多个@Published
属性。 它还从API进行一些远程提取:
class CharacterStore: ObservableObject {
@Published private(set) var isLoading = false
// Fetches some stuff from a remote api
func fetch() {
self.isLoading = true
myService.getCharacters { (result) in
DispatchQueue.main.async {
self.isLoading = false
}
}
}
}
最后这是我希望在其中显示带有ContentView
内容的LoadingView
的视图。 当然,我在显示此视图之前设置@EnvironmentObject
。
struct ContentView: View {
@EnvironmentObject var charStore: CharacterStore
var body: some View {
LoadingView(isShowing: self.$charStore.isLoading) { // Here I get the error
// Show some Content here
Text("")
}
}
}
问题是我想绑定self.$charStore.isLoading
到LoadingView
。 在这一行中我得到以下错误:
无法推断通用参数“主题”
我试过几种方法,但这些都没有用。 顺便说一句:如果我在ContentView
使用@State
属性,它就像这样工作正常:
struct ContentView: View {
@EnvironmentObject var charStore: CharacterStore
@State var loads: Bool = false
var body: some View {
LoadingView(isShowing: self.$loads) { // Here I get no error
// Show some Content here
Text("")
}
}
}
我错过了一件事吗? 如果您需要进一步的信息,请告诉我如果需要我可以提供更多内容。
谢谢您的帮助!
由于您的LoadingView
不会修改.isLoading,因此您无需将其作为绑定传递:
LoadingView(isShowing: self.$charStore.isLoading)
相反,删除@Binding
中的LoadingView
:
struct LoadingView<Content>: View where Content: View {
var isShowing: Bool
...
并像这样创建它(删除美元符号):
LoadingView(isShowing: self.charStore.isLoading) { ... }
相反,如果你坚持传递绑定,那么你需要删除private(set)
:
@Published private(set) var isLoading = false
你能不能做到以下几点:
用ContentView使用的@EnvironmentObject替换@Binding。
struct LoadingView<Content>: View where Content: View {
@EnvirontmentObject var charStore: CharacterStore // added
//@Binding var isShowing: Bool // removed
var content: () -> Content
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .center) {
self.content()
.disabled(self.$charStore.isLoading) // Changed
.blur(radius: self.$charStore.isLoading ? 3 : 0) // Changed
VStack {
Text("Loading...")
ActivityIndicator(isAnimating: .constant(true), style: .large)
}
.frame(width: geometry.size.width / 2,
height: geometry.size.height / 5)
.background(Color.white)
.foregroundColor(Color.primary)
.cornerRadius(5)
.opacity(self.$charStore.isLoading ? 1 : 0) // Changed
}
}
}
}
当然,您还必须从ContentView中的LoadingView()初始值设定项中删除isShowing参数。
如果我错了请纠正我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.