TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 24 challenge

Forums > SwiftUI

Hello!

At the second challenge «Go back to project 1 and use a conditional modifier to change the total amount text view to red if the user selects a 0% tip.»

I can't figure out how to toggle @State private var to true when user selects 0%.

Please help me

3      

When user select 0% tip from array but have the modiflier to change foreground color to red with

.foregroundColor(tipPercentage == 4 ? .red : .black)

where 4 is the number in array or you can do

var noTipSelected: Bool {
        let tipSelection = Double(tipPercentages[tipPercentage]) ?? 0
        if tipSelection == 0 {
            return true
        } else {
            return false
        }
    }

then add

.foregroundColor(noTipSelected ? .red : .black)

3      

@NigelGee can you explain this solution? I can see that it works but I don't undertand why. Thank you!

.foregroundColor(tipPercentage == 4 ? .red : .black)

3      

Because tipPercentage == 4 is the forth in the Array of tipPercentages so when that is selected it will be true therfore turn red

3      

thanks for the help!

3      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.