英文:
I'm writing code for a small app. But I keep getting an error
问题
我正在为一个小应用程序编写代码。但我一直遇到一个错误
我一直遇到的错误是“传递给参数类型为'FormStyleConfiguration'的尾随闭包,该参数不接受闭包”。请帮助我解决这个问题。
我找不到任何解决办法。如果你们想要更多细节,我可以提供。但我需要一个解决方案。
Form {
Section {
TextField(text: replicatestuff.$prompt,
prompt: Text("输入要显示图像的提示"),
axis: .vertical,
label: {})
.disabled(replicatestuff.prediction?.status.terminated == false)
.submitLabel(.go)
.onSubmit(of: .text) {
Task {
try! await replicatestuff.generate()
}
}
}
if let prediction = replicatestuff.prediction {
ZStack {
Color.clear
.aspectRatio(1.0, contentMode: .fit)
switch replicatestuff.prediction.status {
case .starting, .processing:
VStack{
ProgressView("生成中...")
.padding(32)
Button("取消") {
Task { try await replicatestuff.cancel() }
}
}
case .failed:
Text(replicatestuff.prediction.error?.localizedDescription ?? "未知错误")
.foregroundColor(.red)
case .succeeded:
if let url = replicatestuff.prediction.output?.first {
VStack {
AsyncImage(url: url, scale: 2.0, content: { phase in
phase.image?
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(32)
})
ShareLink("导出", item: url)
.padding(32)
}
}
case .canceled:
Text("预测已取消")
.foregroundColor(.secondary)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.padding()
.listRowBackground(Color.clear)
.listRowInsets(.init())
}
}
Spacer()
英文:
I'm writing code for a small app. But I keep getting an error
The error I keep getting is "Trailing closure passed to a parameter of type 'FormStyleConfiguration' that does not accept a closure". Please help me out.
I can't find any solution for it. i can provide more detail if you guys want me to. but i need a solution for it.
Form {
Section {
TextField(text: replicatestuff.$prompt,
prompt: Text("Enter a prompt to display an image"),
axis: .vertical,
label: {})
.disabled(replicatestuff.prediction?.status.terminated == false)
.submitLabel(.go)
.onSubmit(of: .text) {
Task {
try! await replicatestuff.generate()
}
}
}
if let prediction = replicatestuff.prediction {
ZStack {
Color.clear
.aspectRatio(1.0, contentMode: .fit)
switch replicatestuff.prediction.status {
case .starting, .processing:
VStack{
ProgressView("Generating...")
.padding(32)
Button("Cancel") {
Task { try await replicatestuff.cancel() }
}
}
case .failed:
Text(replicatestuff.prediction.error?.localizedDescription ?? "Unknown error")
.foregroundColor(.red)
case .succeeded:
if let url = replicatestuff.prediction.output?.first {
VStack {
AsyncImage(url: url, scale: 2.0, content: { phase in
phase.image?
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(32)
})
ShareLink("Export", item: url)
.padding(32)
}
}
case .canceled:
Text("The prediction was canceled")
.foregroundColor(.secondary)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.padding()
.listRowBackground(Color.clear)
.listRowInsets(.init())
}
}
Spacer()
答案1
得分: 1
你在开始时将(假定的)可选的 replicateStuff.prediction
强制转换为一个非可选的常量 prediction
:
if let prediction = replicatestuff.prediction {
但是在代码的后续部分,你尝试使用仍然是可选类型的 replicateStuff.prediction
:
switch replicatestuff.prediction.status {
这是不正确的,因为我假设 replicateStuff.prediction
是一个可选的。所以你应该在所有地方使用非可选的 prediction
:
prediction.status {
希望这解决了你的问题。
英文:
You cast the (assumed) optional replicateStuff.prediction
to a non-optional constant called prediction
at the start:
if let prediction = replicatestuff.prediction {
but then further in the code you attempt to use the replicateStuff.prediction
which is still an optional type.
switch replicatestuff.prediction.status {
That can't be since replicateStuff.prediction
is an optional (I presume). So you should be using the non-optional prediction
everyewhere instead
prediction.status {
I hope this ends up being your issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论