[ad_1]
I am new coding with Swift and I am doing YouTube’s newbie tasks to study, and at present I am constructing a tip calculator, however I’ve the following errors:
-
Non-constant vary: argument have to be an integer literal
-
Occasion member ‘tipPercentages’ can’t be used on sort ‘ContentView’; did you imply to make use of a worth of this kind as an alternative?
And that is my code:
import SwiftUI
struct ContentView: View {
@State personal var checkAmount = ""
@State personal var numberPeople = ""
@State personal var tipPercentage = 2
let tipPercentages = [10, 15, 20, 25, 0]
var totalPerPerson: Double {
let peopleCount = Double(numberPeople) ?? 0
let tipSelection = Double(tipPercentages[tipPercentage])
let orderAmount = Double(checkAmount) ?? 0
let tipVaule = (orderAmount / 100) * tipSelection
let grandTotal = orderAmount + tipVaule
let amountPerPerson = grandTotal / peopleCount
return amountPerPerson
}
var totalAmount: Double {
let tipSelection = Double(tipPercentages[tipPercentage])
let orderAmount = Double(checkAmount) ?? 0
let tipValue = (orderAmount / 100 ) * tipSelection
let grandTotal = orderAmount + tipValue
return grandTotal
}
var physique: some View {
NavigationView {
Type{
Part{
TextField("Quantity", textual content: $checkAmount)
.keyboardType(.decimalPad)
TextField("Variety of folks:", textual content: $numberPeople).keyboardType(.numberPad)
}
Part(header: Textual content("How a lot tip do you need to depart")) {
Picker("Tip Share", choice: $tipPercentage){
ForEach(0..<tipPercentages.rely){
Textual content("(Self.tipPercentages[$0])%")
}
}
.pickerStyle(SegmentedPickerStyle(])
}
Part(header: Textual content("Quantity per individual")){
Textual content("$(totalPerPerson, specifier: "$.2f")")
}
Part(header: Textual content ("Complete quantity of the test")){
Textual content("$(totalAmount, specifier: "N.2f")")
.foregroundColor(tipPercentage == 4 ? .crimson : .main)
}
.navigationBarTitle("SplitTip")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
[ad_2]

