100 Days of SwiftUI - Day 18
day 16, 17에 진행한 어플 예제를 리뷰하고 연습문제를 해결한다.
챌린지
- “Amount per Person”이라는 헤더를 세번째 섹션에 추가한다.
- 총 금액에 팁 비율을 더한 최종 금액을 표시하는 섹션을 추가한다.
- 팁 비율을 0%~100%에서 하나 선택할 수 있도록 변경. 단 선택 시 새로운 창으로 이동해야 한다.
결과
*정답이 아닐 수도 있음
1번
Section("Amount per Person") {
Text(totalPerPerson,
format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
}
2번
//계산된 프로퍼티를 추가하고
var grandTotal: Double {
let tipSelection = Double(tipPercentage)
let tipValue = checkAmount / 100 * tipSelection
let grandTotal = checkAmount + tipValue
return grandTotal
}
// Form 안에 추가
Section("Total Price") {
Text(grandTotal,
format:.currency(code: Locale.current.currency?.identifier ?? "USD")
)
}
3번
Picker("Number of people", selection: $numberOfPeople) {
ForEach(2..<100) {
Text("\($0) people")
}
}
.pickerStyle(.navigationLink)
참고: https://www.hackingwithswift.com/100/swiftui/19