UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Why is my calculation not working?

Forums > SwiftUI

Below is my first stab at making a very simple program. I can't seem to get the pounds per gallon calculation to function. So sadly I need help here is my code:

import SwiftUI

struct ContentView: View {
    @State private var sampleVolume = 900.00
    @State private var sampleWeight = 1000.00
    @State private var sampleArea = 0.000
    let weightofWater = 8.331

    var sPg : Double {
        let poundsPerGallon = sampleArea * weightofWater
        print(poundsPerGallon)
      return poundsPerGallon
    }

    var body: some View {
        NavigationView {

            Form{
                Group {
                Section {
                    TextField("Enter sample volume", value: $sampleVolume, format: .number)

                } header:
                { Text("Enter sample volume in Milliliter")}

                Section{
                    TextField("Enter sample net weight", value: $sampleWeight, format: .number)

                } header:
                { Text("Enter sample weight in grams")}

                Section {
                    let sampleArea = (sampleWeight / sampleVolume)
                    Text("\(sampleArea)")
                } header:
                { Text("Sample result g/ml:")}

                Section {
                    Text(sPg, format: .number)
                }

            }
            .padding(.horizontal)
            .navigationTitle("Specific Gravity Calc")
            }
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()

    }
}

2      

Hi You do not change @State private var sampleArea = 0.0 and therfore this will var sPg : Double wil be zero.

I think you were trying to change it here 👇, however this sampleArea only works in the closure and does not change the @State var sampleArea.

Section {
  let sampleArea = (sampleWeight / sampleVolume)
  Text("\(sampleArea)")
} header: { 
  Text("Sample result g/ml:")
}

You be better of using another computed property to calulate the sampleArea

var sampleArea: Double {
    sampleWeight / sampleVolume
}

var sPg : Double {
   sampleArea * weightOfWater
}

then in the body

Section {
    Text("\(sampleArea)")
} header: {
    Text("Sample result g/ml:")
}

Section {
    Text(sPg, format: .number)
}

2      

Thanks again for the assist, hope you have a great day!

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.