我在为一个小应用程序编写代码。但是我一直遇到一个错误。

huangapple go评论69阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月6日 20:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定