Hi Team, I am very new and may not be explaining myself very well here but hope someone can help.
I have a section of my app where the user will input a figure (I.e. weekly income amount) and I want to calculate the annual amount ( * 52) then store both figures to be used in other screens oif the app.
Below is how I am passing the data through bindings:
import Foundation
struct IncomeDetails: Identifiable, Codable {
let id: UUID
var incomeDetails: String
var incomeFrequency: Period
var incomeAmount: Int
var incomeAnnual: Int
init(id: UUID = UUID(), incomeDetails: String, incomeFrequency: Period, incomeAmount: Int, incomeAnnual: Int) {
self.id = id
self.incomeDetails = incomeDetails
self.incomeFrequency = incomeFrequency
self.incomeAmount = incomeAmount
self.incomeAnnual = incomeAnnual
}
}
extension IncomeDetails {
struct Data {
var incomeDetails: String = ââ
var incomeFrequency: Period = .Weekly
var incomeAmount: Int = 0
var incomeAnnual: Int = 0
}
var idata: Data {
Data(incomeDetails: incomeDetails, incomeFrequency: incomeFrequency, incomeAmount: incomeAmount, incomeAnnual: incomeAnnual)
}
mutating func update(from idata: Data) {
incomeDetails = idata.incomeDetails
incomeFrequency = idata.incomeFrequency
incomeAmount = Int(idata.incomeAmount)
incomeAnnual = Int(idata.incomeAnnual)
}
init(idata: Data) {
id = UUID()
incomeDetails = idata.incomeDetails
incomeFrequency = idata.incomeFrequency
incomeAmount = Int(idata.incomeAmount)
incomeAnnual = Int(idata.incomeAnnual)
}
}
extension IncomeDetails {
static let sampledata: [IncomeDetails] =
[
IncomeDetails(incomeDetails: âWagesâ, incomeFrequency: .Weekly, incomeAmount: 1000, incomeAnnual: 52000),
IncomeDetails(incomeDetails: âDividendsâ, incomeFrequency: .Quarterly, incomeAmount: 500, incomeAnnual: 2000)
]
}
Then this is the code which I am using to have users input data: (I have bolded the area which I have having trouble with):
import SwiftUI
struct IncomeEditView: View {
@Binding var data: IncomeDetails.Data
@State private var newIncomeDetails = ââ
@State private var newIncomeAmount = ââ
@State private var isPresentingEditView = false
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: ( if data.incomeFrequency == âAnnuallyâ {**
** data.incomeAmount**
** }**
** else if data.incomeFrequency == âQuarterlyâ {**
** data.incomeAmount * 4**
** }**
** ), format: .number)**
** .keyboardType(.numberPad)**
** .disabled(true)**
}
}
}
}
struct IncomeEditView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
IncomeEditView(data: .constant(IncomeDetails.sampledata[0].idata))
}
}
}
The errors which I am getting are:
Expected expression in list of expressions
No exact matches in call to initilizer
if anyone can help that would be awesome.
Thanks