想要在等式中使用@Binding变量吗

想要在等式中使用@Binding变量吗,binding,initialization,swiftui,Binding,Initialization,Swiftui,上面是我的代码,我确实在另一个视图中使用@State正确定义了@Binding变量 我想做的是在方程式中使用“rate”来定义“apr/12”。我想我以前在Xcode的早期版本中解决过这个问题,但现在我得到的是:不能在属性初始值设定项中使用实例成员'apr';属性初始值设定项在“self”可用之前运行。 我知道我在使用绑定类型,但即使使用Double更改类型,我仍然会出错。任何帮助都将不胜感激 似乎您正试图在初始化完成之前访问结构中的一个属性,例如: struct FinanceOutput:

上面是我的代码,我确实在另一个视图中使用@State正确定义了@Binding变量

我想做的是在方程式中使用“rate”来定义“apr/12”。我想我以前在Xcode的早期版本中解决过这个问题,但现在我得到的是:不能在属性初始值设定项中使用实例成员'apr';属性初始值设定项在“self”可用之前运行。
我知道我在使用绑定类型,但即使使用Double更改类型,我仍然会出错。任何帮助都将不胜感激

似乎您正试图在初始化完成之前访问结构中的一个属性,例如:

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       
}
您可以从正文中访问“rate”属性,例如:

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

提供的代码快照在我这边正确编译。请显示生成上述错误的代码。我检查了您的答案,得到了apr/费率等于0的未定义答案。我将私人var比率:Double=0.0改为私人var比率:Double=12.0,但仍然没有运气。我想做的是有一个变量,我可以使用@Binding变量计算汽车付款。在上面的示例中,我使用了固定值,而不是数字的真实示例。另外,我只将apr除以10.0/12.0=0.833333。如果你得到一个0的值,也许你正在使用一个双精度到整数的转换,并在某个地方进行初始化[比如:Intapr/rate]。试着只使用上面的例子,而不做任何修改,以了解你应该在哪里改变什么。注意:输入类是一个可以用实数初始化的类,然后你可以通过一个方法在FinanceOutput的主体中创建你的方程逻辑。使用你的答案和另一个解决了这个问题:。感谢您提供的信息。谢谢。我很高兴能帮上忙。你可以给我的答案打分,并将其标记为已解决;
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)")
    }
}