Hi Team,
The below code is where I have a form to input but I want the “Annual Amount” to have a calculated value. So I have disabled the edit function for this section and tried to have .onAppear be my calculated value. But I am having issues with the .onAppear which is giving me the error “Expression of type ‘Binding’ is unused.”.
Any help on how to resolve this would be awesome.
Thanks
import SwiftUI
struct IncomeEditView: View {
@Binding var data: IncomeDetails.Data
private var computedIncomeAmount: Binding<Int> {
Binding(
get: {
switch data.incomeFrequency {
case .Daily:
return data.incomeAmount * 365
case .Weekly:
return data.incomeAmount * 52
case .Fortnightly:
return data.incomeAmount * 26
case .Monthly:
return data.incomeAmount * 12
case .Quarterly:
return data.incomeAmount * 4
case .Annually:
return data.incomeAmount
}
},
set: { _ in }
)
}
var body: some View {
Form {
Section(header: Text("Details")) {
TextField("Details", text: $data.incomeDetails)
}
Section(header: Text("Frequency")) {
PeriodPicker(selection: $data.incomeFrequency)
}
Section(header: Text("Amount")) {
TextField("Amount", value: $data.incomeAmount, format: .number)
.keyboardType(.numberPad)
}
Section(header: Text("Annual Amount")) {
TextField("Amount", value: $data.incomeAnnual, format: .number)
.onAppear(perform: {computedIncomeAmount})
.keyboardType(.numberPad)
.disabled(true)
}
}
}
}
struct IncomeEditView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
IncomeEditView(data: .constant(IncomeDetails.sampledata[0].data))
}
}
}