我有两个文本字段,用于更改两个@State变量(即startingMileage和EndingMileage)的值,以及一个步进器,用于更改名为燃料添加的第三个@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。
有什么想法吗?
答案 0 :(得分:0)
问题在于,milesPerGallon
的分配与用于构建视图层次结构的参数的“函数构建器语法”不能很好地配合。如果将@State var milesPerGallon
替换为局部变量(它是 is ,它不携带视图所依赖的状态,而只是一个中间值),则会变得更加明显:
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'
有关函数生成器语法的更多信息,请参见What enables SwiftUI's DSL?(该链接也提供了文档链接)。
最简单的解决方案是避免局部变量并直接对文本进行插值:
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)
}
可能还有其他解决方法,但这是我到目前为止提出的。