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

SOLVED: Project 4, Part 3 Challenge | When are computed properties "recomputed"?

Forums > 100 Days of SwiftUI

Change the user interface so that it always shows their recommended bedtime using a nice and large font. You should be able to remove the “Calculate” button entirely.

I've created a computed string property, using the code that was called when we hit the calculate button. It works just like I was hoping, but I'm not entirely sure why it works.

How does SwiftUI know to "recompute" the property each time I change one of the inputs? Is the entire view being rebuilt each time one of the pickers/steppers is changed, causing that property to be accessed again?

2      

Yes, when an @State property is changed the entire view (struct) is recreated ...

2      

Nick wonders about computed vars:

How does SwiftUI know to "recompute" the property each time I change one of the inputs?

Gakkie has the correct answer!
Here's a bit of code to illustrate some of the concepts. Paste into Playgrounds for jollies!

struct LightBulb: View {
    var lightIsOn: Bool

    var body: some View {
        Circle()
            .frame(width: 100, height: 100)
            // randomColor is recalculated
            // whenever this view is drawn on screen.
            .foregroundColor(lightIsOn ? randomColor : .white)
    }
    let bulbColors = [Color.teal, .indigo, .yellow, .red]

    // computed var recalculates whenever it is
    // needed in the code above.
    var randomColor: Color {
        (bulbColors.randomElement()!.opacity(0.4))
    }
}

struct LightSwitch: View {
    @State private var switchOne = true
    @State private var switchTwo = false

    // computed var recalculates whenever
    // the view below is redrawn
    var bothOn: Bool {
        switchOne && switchTwo
    }

    var body: some View {
        VStack {
            VStack {
                Toggle("Switch One", isOn: $switchOne) // trigger redraw
                Toggle("Switch Two", isOn: $switchTwo) // trigger redraw
            }.padding().frame(width: 200, height: 100)
            Divider()
            HStack (spacing: 10){
                // When do these redraw themselves?
                LightBulb( lightIsOn: switchOne )
                LightBulb( lightIsOn: switchTwo )
                ZStack {
                    LightBulb( lightIsOn: bothOn    )
                    Text(bothOn ? "Recalculated" : "" ).font(.caption)
                }
            }
            .padding()
            .animation(.easeIn,                  value: switchTwo)
            .animation(.easeIn(duration: 1.25),  value: switchOne)
        }
    }
}

// Run this line in Playgrounds.
PlaygroundPage.current.setLiveView( LightSwitch() )

3      

@Obelix, as elaborate and fantastic as always ✌️

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.