2

*I have these three @State variables:

@State var mhzValue : Float = 0 @State var mhzValueStep : Float = 0 @State var TotalFrequency : Float = 0 

And although they mhzValue & mhzValueStep display on in my app..

I wish to add them together..

Example: var TotalFrequency = mhzValue + mhzValueStep

But I just cannot get it to work..

Any suggestions please.. I am a very new to this... Thanks !*

import SwiftUI struct DipoleView : View { @State var mhzValue : Float = 0 @State var mhzValueStep : Float = 0 @State var TotalFrequency : Float = 0 var body: some View { VStack { //Slider one Text("Slide to select Frequency") .font(.headline) .color(.blue) .padding(.leading, -130.0) Slider(value: $mhzValue, from: 1, through: 55, by: 1) .padding(.horizontal) Text("\(Int(mhzValue)) in Mhz") .font(.title) .fontWeight(.semibold) .color(.blue) // Slider Two Text("Slide to select Decimal Point") .font(.headline) .color(.orange) .padding(.leading, -130.0) Slider(value: $mhzValueStep, from: 1, through: 999, by: 0.1) .padding(.horizontal) Text(".\(Int(mhzValueStep)) in Mhz") .font(.title) .fontWeight(.semibold) .color(.orange) Text(" Frequency: \(Int(mhzValue)).\(Int(mhzValueStep)) Mhz") .font(.largeTitle) .fontWeight(.medium) .color(.white) .padding(10) .background(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/) .cornerRadius(10.0) .shadow(radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/) // Load Image View Spacer() ImageView() .padding(.bottom, 40) } } } #if DEBUG struct DipoleView_Previews : PreviewProvider { static var previews: some View { DipoleView() } } #endif 
2
  • Sorry about the very bad formatting of the text. Commented Jun 28, 2019 at 2:51
  • Nothing to worry about, formatting looks fine! Commented Jun 28, 2019 at 4:08

1 Answer 1

4

You don't need to declare TotalFrequency as a State variable cause both the mhzValue and mhzValueStep are declared as State var and you just need the sum of this two. Rather you can declare TotalFrequency as a computed var. Here is a working version of the code.

import SwiftUI struct ContentView : View { @State var mhzValue : Float = 0 @State var mhzValueStep : Float = 0 private var TotalFrequency : Float { return mhzValue + mhzValueStep } var body: some View { VStack { //Slider one Text("Slide to select Frequency") .font(.headline) .color(.blue) .padding(.leading, -130.0) Slider(value: $mhzValue, from: 1, through: 55, by: 1) .padding(.horizontal) Text("\(Int(mhzValue)) in Mhz") .font(.title) .fontWeight(.semibold) .color(.blue) // Slider Two Text("Slide to select Decimal Point") .font(.headline) .color(.orange) .padding(.leading, -130.0) Slider(value: $mhzValueStep, from: 0, through: 1, by: 0.1) .padding(.horizontal) Text("\(mhzValueStep) in Mhz") .font(.title) .fontWeight(.semibold) .color(.orange) Text(" Frequency: \(TotalFrequency) Mhz") .font(.largeTitle) .fontWeight(.medium) .color(.white) .padding(10) .background(/*@START_MENU_TOKEN@*/Color.blue/*@END_MENU_TOKEN@*/) .cornerRadius(10.0) .shadow(radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/) // Load Image View Spacer() // Image() // .padding(.bottom, 40) } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much... that works... Now I can move forward on this.... I have been playing around with Xcode and Swift for a couple of years and continue to hit brick walls.. People like you are so helpful and I really appreciate it.. I'm in my 60's and find it hard now to remember... Thank you again !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.