Кажется, что вы пытаетесь получить доступ к одному из своих свойств в своей структуре до завершения init (), например:
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)")
}
}
Вы можете получить доступ к свойству 'rate' из тела, например:
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)")
}
}