UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day24 Doubt

Forums > 100 Days of SwiftUI

Hello,

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 ? Color.red : Color.black)

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?

Thanks

2      

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?

3      

@Bnerd  

.foregroundColor(tipPercentage == 0 ? Color.red : Color.black)

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?

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.