I am developing a demo to practice swiftUI and Combine, I got error when I add group into a view as follows:
var body: some View {
HStack {
// TODO: Add image view
Group{
switch viewModel.state{
case .loading:
print("")
case .failed(let error):
print("")
case .sucess(let image):
print("")
}
}
VStack(alignment: .leading, spacing: 4) {
Text(article.title ?? "")
.foregroundColor(.black)
.font(.system(size: 18, weight: .semibold))
Text(article.source ?? "")
.foregroundColor(.gray)
.font(.system(size: 12, weight: .regular))
}
}
}
Photo is as follows:
enter image description here
It’s strange, because I did the same in another view, it works well…
By the way, I want to use group because I want to use onAppear(perform: viewModel.loadImage(from: article.image))
as follow:
Group{
switch viewModel.state{
case .loading:
print("")
case .failed(let error):
print("")
case .sucess(let image):
print("")
}
}.onAppear(perform: viewModel.loadImage(from: article.image))
My object is to download one article image when the articleView(name of my class) appears, I think I can not use a function with parameters in onAppear? and If I want to download the article image when this articleView appear, How can I do it? can I use task
to replace
onAppear
? thanks a lot
I tried to organize my var body: some View
and searched information in internet I can not find it, and the error Failed to produce diagnostic for expression; please submit a bug report is not clear