表达式类型不明确,没有更多上下文问题

时间:2020-02-05 12:40:14

标签: xcode swiftui

我是菜鸟swiftUI程序员,刚刚入门,需要帮助。我在代码中收到了错误消息“表达式类型不明确,没有更多上下文”,但是我不知道为什么。错误消息出现在随后输入的TextbI的字体和字体大小的行上。每当我尝试将变量声明为Integer进行计算时,都会发生错误。当我转换回字符串时,该消息消失。为什么?这是我的代码:

import SwiftUI





struct ContentView: View {
@State private var weight: Double = 0
@State private var height: Double = 0
@State private var bmi: Double = 0

var body: some View {

    VStack{

        Text("BMI Calculator")
            .font(.largeTitle)

        HStack{
            Text("Enter Height")
                .font(.custom("DIN Condensed", size: 20))


            TextField("Height (M) " , text: $height)
                .font(.custom("DIN Condensed", size: 20))
                .keyboardType(.numberPad)
        }

        HStack{
            Text("Enter Weight")
                .font(.custom("DIN Condensed", size: 20))
            TextField("Weight (KG) " , text: $weight)
                .font(.custom("DIN Condensed", size: 20))
                .keyboardType(.numberPad)
        }


        Button(action: {
            //Action of the button here
            let weightint = Int(weight.text!)
            let heightint = Int(height.text!)

            bmi = heightint / (weightint * weightint)
            print("testting")


        }) {
            Text("CALCULATE")
                .font(.custom("DIN Condensed", size: 60))
                .background(Color.red)
        }
    }

        //output

}
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

您在变量方面犯了几个错误:

1)文本字段需要一个String,而不是double作为变量。

2)一些自我。哪里也缺少

3)Tipp:不幸的是,SwiftUI中的错误消息并不总是指向您的实际问题-尝试注释掉“新”代码,直到再次成功为止(您可以将其编译为没有错误),然后逐步取消对新代码的注释排线并观看,当您发生错误时...

struct ContentView: View {
    @State private var weight: Double = 0
    @State private var height: Double = 0
    @State private var bmi: Double = 0

    @State private var textHeight : String = ""
    @State private var textWeight : String = ""

    var body: some View {

        VStack{

            Text("BMI Calculator")
                .font(.largeTitle)

            HStack{
                Text("Enter Height")
                    .font(.custom("DIN Condensed", size: 20))


                TextField("Height (M) " , text:$textHeight)
                    .font(.custom("DIN Condensed", size: 20))
                    .keyboardType(.numberPad)
            }

            HStack{
                Text("Enter Weight")
                    .font(.custom("DIN Condensed", size: 20))
                TextField("Weight (KG) " , text: $textWeight)
                    .font(.custom("DIN Condensed", size: 20))
                    .keyboardType(.numberPad)
            }


            Button(action: {
                //Action of the button here
                let weightint = self.weight
                let heightint = self.height

                self.bmi = heightint / (weightint * weightint)
                print("testting")


            }) {
                Text("CALCULATE")
                    .font(.custom("DIN Condensed", size: 60))
                    .background(Color.red)
            }
        }

    }
}