Post

Replies

Boosts

Views

Activity

Reply to Button to Multiple Two Variables and Return Result
Honestly, thank you for that last response. Here is what I did next: 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?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Button to Multiple Two Variables and Return Result
> 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? 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?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to Button to Multiple Two Variables and Return Result
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: 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: 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.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21