Post

Replies

Boosts

Views

Activity

Button to Multiple Two Variables and Return Result
I'm working on creating a simple Random Sample app for my job that will allow you to get the tonnage that an asphalt sample should be taken. I have created a random number generator in the app, managed to get it to multiply by 1000 (required number for calculations) and get those numbers to show up on the screen in the app. The final part that I need to do now is get that number to be added to a number that is put in by the user in a text field. Here is a snip of my code so far that keeps giving an error: struct BaseOGIntView: View {       @StateObject var equation = BOIEquations()        var sublotSampTons = BOIEquations.shared.sublotSampTons        var body: some View {      // Display Random Number      VStack {        let storedRanSampTons = BOIEquations.shared.ranSampTons                Form {          Section{            Text("Random Number: \(BOIEquations.shared.randomNumber, specifier: "%.3f")")         }          Section {            Text("Sublot Tons: \(storedRanSampTons, specifier: "%.0f")")            TextField("Sublot Tons", value: $equation.sublotSampTons, formatter: NumberFormatter())            Button(action: {              func sublotTonnage(_ number: Double) -> Double {                var number = $equation.sublotSampTons + storedRanSampTons                return number             }           }) {              Text("Generate Random Sample")           }         }          Text("Random Sample Tonnage: \(sublotTonnage) ")       }     }   } } The function I am trying to run as an action inside the button is giving an error when trying to add the two variables together. That error is "Binary operator + cannot be applied to operands of type Binding<Double?> and Double" The other error is when I try to call the results line 29, but I am guessing that is because I need to get the results of that button to go into another variable to be able to call and print there. The error received is "Cannot find sublotTonnage in scope". Any help would be greatly appreciated as this is the last bit of code I need to figure out for the initial version of this app. Thank you in advance!
10
0
927
Jan ’21