AsyncImage.不能在Phase闭包中使用switch语句?



当我在asyncImage中使用switch语句时,它给了我这个错误。

后闭包传递给不接受闭包的'CGFloat'类型参数

我知道docs使用if let语句来提取图像并得到下面的错误。

我代码:

AsyncImage(url: URL(string:stringURL)) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.scaledToFit()
case .failure():
EmptyView()
}
}

根据Apple Docs:

AsyncImage(url: URL(string: "https://example.com/icon.png")) { phase in
if let image = phase.image {
image // Displays the loaded image.
} else if phase.error != nil {
Color.red // Indicates an error.
} else {
Color.blue // Acts as a placeholder.
}

我的问题是为什么我不能在这里使用switch语句。这个误差到底是什么意思?

谢谢。

你可以使用一个switch语句,使用这个,为我工作:

case .failure(_):  // <-- here 

下面是我用来显示我的答案有效的代码:

struct ContentView: View {

@State private var stringURL = "https://upload.wikimedia.org/wikipedia/commons/e/ef/Red-billed_gull%2C_Red_Zone%2C_Christchurch%2C_New_Zealand.jpg"

var body: some View {
AsyncImage(url: URL(string: stringURL)) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image.resizable().scaledToFit()
case .failure(_):  // <-- here
EmptyView()
@unknown default:
Image(systemName: "exclamationmark.icloud")
}
}
}

}

最新更新