BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

Day24 Doubt

Forums > 100 Days of SwiftUI


I lost my self in the first test

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 find the solution like this with this solution

.foregroundColor(tipPercentage == 0 ? :

But what i would like to do is to assign to the struct a:

@State private var useRedText = false

and togle the value when the picker is on 0, but i can't find the way to checke the value of the picker slection and toggle my var to fals state when 0 is selected.

Some one could help to find the solution?



Instead of using an @State variable for useRedText, try using a computed property. Inside the computed property, you would check the value of the var used to track the Picker's value and return true or false accordingly. Give it a try!

But why use a computed property instead of tagging useRedText with @State?

State properties are for things that should trigger a re-evaluation and possibly a rendering of the View when they change. You aren't changing the value of useRedText and then expecting a rendering of your View; you are already rendering the View and, while doing so, using the value of useRedText to determine what color should be used to draw the text. Make sense?



.foregroundColor(tipPercentage == 0 ? :

This works perfectly. Why you should keep it? Code efficiency, you are solving a need with one line of code. Making a computed variables is an idea but you see the extra line of code, correct?


Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

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.