0
struct FinanceOutput: View { @Binding var price: Double @Binding var down: Double @Binding var apr: Double private var rate: Double = 0.0 init(price: Binding<Double>, down:Binding<Double>, apr:Binding<Double>, of rate: Double ) { self._price = price self._down = down self._apr = apr self.rate = rate } 

The above is my code and I do have the @Binding variables defined properly in another view--using @State.

What I'm trying to do is use 'rate' in an equation to define 'apr/12'. I think I worked it out before in an earlier version of Xcode but now all I get is: "Cannot use instance member 'apr' within property initializer; property initializers run before 'self' is available." I know I am using Binding types but even when I change type using Double() I still get errors. Any help would be appreciated.

1
  • The provided code snapshot compiled correctly at my side. Please show the code the generates mentioned error. Commented Jan 15, 2020 at 8:06

1 Answer 1

1

It seems like you're trying to access one of your properties within your struct before init() is complete, e.g.:

struct FinanceOutput: View { @Binding var price: Double @Binding var down: Double @Binding var apr: Double private var rate: Double = 0.0 init(price: Binding<Double>, down: Binding<Double>, apr: Binding<Double>, of rate: Double) { self._price = price self._down = down self._apr = apr self.rate = rate } apr = rate // error: Cannot use instance member 'apr' within property initializer; property initializers run before 'self' is available. var body: some View { Text("\(apr/rate)") } } 

You can access the 'rate' property from within the body, e.g.:

let input = Input(price: 10.0, down: 10.0, apr: 10.0) struct FinanceOutput: View { @Binding var price: Double @Binding var down: Double @Binding var apr: Double private var rate: Double = 0.0 init(price: Binding<Double>, down: Binding<Double>, apr: Binding<Double>, of rate: Double) { self._price = price self._down = down self._apr = apr self.rate = rate } var body: some View { Text("\(apr/rate)") // shows apr/rate properly } } struct ContentView_Previews: PreviewProvider { static var previews: some View { FinanceOutput(price: input.$price, down: input.$down, apr: input.$apr, of: 12.0) } } struct Input: View { @State var price: Double @State var down: Double @State var apr: Double var body: some View { Text("\(price), \(down), \(apr)") } } 
Sign up to request clarification or add additional context in comments.

4 Comments

I checked out your answer and I get an undefined answer for (apr/rate)--rate equals 0. I changed "private var rate: Double = 0.0" to "private var rate: Double = 12.0" and still no luck. What I want to do is have a variable where I can calculate a car payment using my @Binding variables.
In the example above I used fixed values, and not real-world examples for the numbers. Also, I only divided apr by the rate ( 10.0 / 12.0 = 0.833333 ). If you get a value of 0, maybe you're using a Double to Int conversion with initialization somewhere [like this: Int(apr/rate)]. Try to only use the example above without any modification to understand where and what should you change. Note: the Input class is the one which can be initialized with real numbers, then you can make your equation logic in the FinanceOutput's body (by a method for example).
Fixed the problem using your answer and another: stackoverflow.com/questions/56800369/swiftui-state-variables. Appreciate the information. Thanks.
I am glad I could help. You can rate my answer, and mark it as solved ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.