简体   繁体   English

如何在 SwiftUI 中的 body 属性内显示函数内的值?

[英]How to show a value present inside a function inside the body property in SwiftUI?

I am making an app that requires me to use an ml model and is performing its calculation to get the output from the ml model inside a function, now I need to display that constant which stores the final output of that ml model inside the body property of my swift UI view so that I can present it inside Text() maybe and modify the way it looks Lil bit all of this code is inside a single swift file我正在制作一个应用程序,它需要我使用 ml 模型并执行其计算以从函数内的 ml 模型获取输出,现在我需要显示该常量,该常量将存储该 ml 模型的最终输出到 body 属性中我的 swift UI 视图,以便我可以在 Text() 中呈现它并修改它的外观Lil bit 所有这些代码都在一个 swift 文件中

here is all the code这是所有的代码

图 1

其余代码

after making the changes it's still showing some error进行更改后,它仍然显示一些错误

struct newView: View {
    
    let model = AudiCar()
    
    @ObservedObject var values: impData
   @State var price = ""
    var body: some View {
        Text("calculated price is " + price)
                    .onAppear {
                        calculatePrice()
    }

    func calculatePrice() {  <- this is where it showing error, saying " Closure containing a declaration cannot be used with function builder 'ViewBuilder' " 
        
       do {
        let AudiCarOutput =  try model.prediction(model: String(values.nameSelection), year: Double(values.yearSelection), transmission: String(values.transmisssionSelec), mileage: Double(values.mileage), fuelType: String(values.fuelSelection))
            
           let price = String(AudiCarOutput.price)

       }

       catch {

       }
 
    }

   
}

struct newView_Previews: PreviewProvider {
    static var previews: some View {
        newView(values: impData())
    }
}

try something like this:尝试这样的事情:

struct NewView: View {
    let model = AudiCar()
    @ObservevedObject var values: impData
    
    @State var price = ""
    
    var body: some View {
        Text("calculated price is " + price)
            .onAppear {
                calculatePrice()
            }
    }
    
    func calculatePrice() {
        do {
            ...
           price = String(AudiCarOutput.price)
        }
        catch {
            ...
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 SwiftUI 结构内的计算属性内使用 for 循环 - How to use for loop inside computed property inside struct in SwiftUI 如何使用 swiftUI 在视图主体内使用变量? - How to use variable inside the body of the view using swiftUI? 如何在 SWIFTUI 主体视图中使用声明的变量 - how can I use a declared variable inside SWIFTUI body view 当模型在@Published 属性内更新时,SwiftUI 触发功能 - SwiftUI trigger function when model updates inside @Published property 如何在 SWIFT UI 的主体内运行 function? - How to run a function inside a body of SWIFT UI? 如何在 SwiftUI 的 Widget 中使用 LinkPresentation Framework 来显示 urlImage 预览? - How to use the LinkPresentation Framework inside a Widget in SwiftUI to show an urlImage preview? 如何在swiftui中的if-condition语句中显示警报视图? - how to show an alert view inside if-condition statement in swiftui? 如何使用可表示协议在SwiftUI中显示UICollectionViewController? - How to show an UICollectionViewController inside SwiftUI using representable protocol? 过滤器 function 内部为每个 SwiftUI - Filter function inside For Each SwiftUI 在 SwiftUI 中将视图声明为视图主体内的变量时出现“函数声明不透明的返回类型 [...]”错误 - "Function declares an opaque return type [...]" error when declaring a view as a variable inside the body of a View in SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM