gpt4 book ai didi

swift - 在 SwiftUI 中使用状态变量作为 func 的输入

转载 作者:行者123 更新时间:2023-12-03 23:51:44 25 4
gpt4 key购买 nike

我有两个文本字段可以更改两个@State 变量的值,即startingMileage 和endingMileage,还有一个步进器可以更改名为fuelAdded 的第三个@State 变量的值。我正在尝试使用用户的输入并进行计算来计算每加仑英里数。在操场上,我的代码按预期工作。但是,它在 SwiftUI 项目中不起作用。

尝试在操场上运行代码如下:

func CalcMPG(start: String, end: String, fuel: Double) -> Int {
let start = Int(start) ?? 1
let end = Int(end) ?? 1
let fuel = Int(fuel)
let mpg = (end-start) / fuel
return mpg
}


var endingMileage:String = "9250"
var startingMileage:String = "9000"
var fuelAdded:Double = 20

let milesPerGallon = CalcMPG(start: startingMileage, end: endingMileage, fuel: fuelAdded)
print("Fuel Efficiency: \(milesPerGallon) mpg")

这按预期工作。

struct ContentView : View {

@State var startingMileage: String = ""
@State var endingMileage: String = ""
@State var fuelAdded: Double = 10
@State var carModel: String = ""
@State var showMPGInfo = false
@State var milesPerGallon: Int = 10

func CalcMPG(start: String, end: String, fuel: Double) -> Int {
let start = Int(start) ?? 1
let end = Int(end) ?? 1
let fuel = Int(fuel)
let mpg = (end-start) / fuel
return mpg
}


var body: some View {
NavigationView {
VStack{
HStack {
Text("Car Model:")
Spacer()
TextField($carModel, placeholder: Text("Toyota Corolla"))
.textFieldStyle(.roundedBorder)
}
HStack {
Text("Starting ODO:")
Spacer()
TextField($startingMileage, placeholder: Text("8000"))
.textFieldStyle(.roundedBorder)
Text("miles")
}

HStack {
Text("Ending ODO:")
Spacer()
TextField($endingMileage, placeholder: Text("9000"))
.textFieldStyle(.roundedBorder)
Text("miles")
}
HStack {
Stepper(value: $fuelAdded, in: 0...20, step: 0.5) {
Text("Fuel Added: \(fuelAdded, specifier: "%0.1f") gallons")
}
}
Button(action: {
self.showMPGInfo.toggle() }){
Text("Show/Hide MPG")
}
if showMPGInfo {
Spacer()
milesPerGallon = CalcMPG(start: startingMileage, end: endingMileage, fuel: fuelAdded)
Text("Fuel effiency: \(milesPerGallon) MPG")
.font(.largeTitle)
}
}.padding()
.navigationBarTitle(Text("Gas Mileage Calculator"))
}
}
}

当用户单击“显示/隐藏 MPG”时。我希望最终得到的文本是“燃油效率:xx MPG”

但是,我收到以下错误:

ContentView.swift:34:19:无法推断复杂的闭包返回类型;添加显式类型以消除歧义

它对我来说没有任何意义......它突出显示了 NavigationView 中的起始 VStack。

有什么想法吗?

最佳答案

问题是分配给 milesPerGallon不适用于用于构建 View 层次结构参数的“函数构建器语法”。如果我们替换 @State var milesPerGallon 会更明显一点通过局部变量(也就是说,它不携带 View 所依赖的状态,只有一个中间值):

if showMPGInfo {
Spacer()
let milesPerGallon = CalcMPG(start: startingMileage,
end: endingMileage, fuel: fuelAdded)
Text("Fuel effiency: \(milesPerGallon) MPG")
.font(.largeTitle)
}

现在编译器错误是
Closure containing a declaration cannot be used with function builder 'ViewBuilder'

For more information about the function builder syntax see What enables SwiftUI's DSL? (which has also links to the documentation).

The simplest solution would be to avoid a local variable and interpolate the text directly:

if showMPGInfo {
Spacer()
Text("Fuel effiency: \(CalcMPG(start: startingMileage, end: endingMileage, fuel: fuelAdded)) MPG")
.font(.largeTitle)
}

其他解决方案是在“立即评估的闭包”中计算文本字段:
if showMPGInfo {
Spacer();
{ () -> Text in
let milesPerGallon = CalcMPG(start: startingMileage,
end: endingMileage,
fuel: fuelAdded)
return Text("Fuel effiency: \(milesPerGallon) MPG")
}()
.font(.largeTitle)
}

或定义一个辅助功能
func resultField(start: String, end: String, fuel: Double) -> Text {
let milesPerGallon = CalcMPG(start: startingMileage, end: endingMileage, fuel: fuelAdded)
return Text("Fuel effiency: \(milesPerGallon) MPG")
}

并将其用作
if showMPGInfo {
Spacer()
resultField(start: startingMileage, end: endingMileage,
fuel: fuelAdded)
.font(.largeTitle)
}

可能还有其他解决方法,但这是我目前想到的。

关于swift - 在 SwiftUI 中使用状态变量作为 func 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56616368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com