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:

Code Block import SwiftUI
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!
Your Button action on line 20...25 does nothing. You declare a local function (line 21...24) inside it, but it is NOT used.

Please clarify on which line the error occurs and show all the definitions (for example BOIEquations) which are involved in the error.

It seems you do not understand the difference of defining a function and calling it. Better learn basics of Swift language first.
The error "Binary operator + cannot be applied to operands of type Binding<Double?> and Double" occurs on line 24 (line 22 if looking at original post) inside the function. I've tried making the function in other parts of the code, but then I start to get errors saying that variables aren't in the scope.

I'm not looking for someone to just type out the code and say "Here is the fix". If you can point me in the right direction, I've got no problem going through tutorials and learning, but at this point, I'm at a loss. I've already completed a couple of SwiftUI and Swift Storyboard courses on Udemy and had a good enough understanding of the basics of Swift language to get this far.

This is the code in BOIEquations.swift:

Code Block import Foundation
import SwiftUI
class BOIEquations: ObservableObject {
   
   static var shared = BOIEquations()
   
   var sublotSampTons: Double? = 0.0
   
   let randomNumber = Double.random(in: 0.001..<1)
   
   var ranSampTons: Double {
     let storedRandomNumber = randomNumber
     let ranSampTonnage = storedRandomNumber * 1000
     return ranSampTonnage
  }
}


This is the full code from the BaseOGIntView.swift:

Code Block import SwiftUI
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) ")
      }
    }
  }
}
struct BaseOGIntView_Previews: PreviewProvider {
   static var previews: some View {
     BaseOGIntView()
  }
}


Thanks again for any help you might give.

I've tried making the function in other parts of the code, but then I start to get errors saying that variables aren't in the scope. 

That's because you have a local variable storedRanSampTons in your VStack, do not move the function definition but move the variable declaration.

had a good enough understanding of the basics of Swift language to get this far. 

I'm not saying you have learnt nothing, but it was not enough seeing your code.
Why are you writing like this if you have enough understanding of the basics?
Code Block
         Text("Random Sample Tonnage: \(sublotTonnage) ")



I'm not saying you have learnt nothing, but it was not enough seeing your code.
Why are you writing like this if you have enough understanding of the basics?

Code Block
Text("Random Sample Tonnage: \(sublotTonnage) ")
That is what was covered in those tutorials as how to pull information from a variable to print on the screen of an app. Is there a better way to do it that those didn't cover because all of them I've done on Udemy and Ray Weinderlich's site all did it that way?






That is what was covered in those tutorials as how to pull information from a variable to print on the screen of an app. Is there a better way to do it that those didn't cover because all of them I've done on Udemy and Ray Weinderlich's site all did it that way?

Seems you are too early for Udemy or Ray Wenderlich's site. I have never seen a site using function name in a String interpolation of Text.
So there lies that issue then. A function can't be used to print items to screen like that. My assumption was that since a variable was being returned into the function, it would be able to be read from by essentially calling the function like that. Thank you for pointing that out.

Thank you for pointing that out.

You are welcome.
Honestly, thank you for that last response. Here is what I did next:
Code Block
struct BaseOGIntView: View {
   
  @StateObject var equation = BOIEquations()
   
   let sublotSampTons = BOIEquations.shared.sublotSampTons
   let storedRanSampTons = BOIEquations.shared.ranSampTons
   
   func sublotTonnage(_ number: Double) -> Double {
     let number = sublotSampTons + storedRanSampTons
     return number
  }
   var body: some View {
     VStack {
      
       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: {
             
          }) {
             Text("Generate Random Sample")
          }
        }
         Text("Random Sample Tonnage:  ")
      }
    }
  }
}

Moving the storedRanSampTons out of the body view and the func out of the button action seems to have fixed a lot of issues. I'm thinking the only thing I have left to do is get the button action to run that function when pressed and to print the result on line 29 with the rest of the string of text. Does that seem to be along the correct line of thinking? Or am I still missing a step somewhere?
Scratch part of that last part... I moved the func back in to the action portion of the button.

Does that seem to be along the correct line of thinking? Or am I still missing a step somewhere?

When you want to show the result of some calculation, you declare an @State variable to hold the result and update it when needed.
Button to Multiple Two Variables and Return Result
 
 
Q