[英]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这是所有的代码
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.