NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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()

    }
}

1      

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)
}

1      

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

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Reply to this topic…

You need to create an account or log in to reply.

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.