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

SOLVED: Binding of Computed Property with Button

Forums > SwiftUI

I would like to link a view with a computed property. The following example shows the principle. I am interested in linking a view element with a computed property. I know that my example task can also be solved without a computed property.

A circle is to appear on the screen. The diameter of the circle is stored in the variable circleSize. The circle colour is stored in the variable circleColor. The diameter is to be increased by 1 with a +button. With the -Button the diameter is to be decreased by 1. If the diameter is larger than 25, the circle should be red. Otherwise, the circle should be blue. The logic for changing the colour is to be done in the setter of the variable "circleNewSize".

With the buttons I always get the following error: Left side of mutating operator isn't mutable: 'self' is immutable

What do I need to change. Thank you in advance.

import SwiftUI

struct ContentView2: View {
    @State private var circleSize: CGFloat = 20
    @State private var circleColor: Color = .blue

    var circleNewSize: CGFloat {
        get {
            return circleSize
        }
        set {
            circleSize = newValue
            if circleSize > 25 {
                circleColor = .red
            } else {
                circleColor = .blue
            }
        }
    }

    var body: some View {
        VStack {
            Circle()
                .foregroundColor(circleColor)
                .frame(width: circleSize, height: circleSize)

            HStack {
                Button(action: {
                    circleNewSize += 1
                }) {
                    Text("+")
                        .font(.largeTitle)
                }

                Button(action: {
                    circleNewSize -= 1
                }) {
                    Text("-")
                        .font(.largeTitle)
                }
            }
        }
    }
}

2      

I don't think this works with a computed property.

What you can do is the following which should work. I'm not on my computer so I can't test.

Drop the computed property completely, you don't need it. For only two colors you don't need the circleColor property, either.

 Circle()
  .foregroundColor(circleSize > 25 ? .blue : .red)
  .frame(width: circleSize, height: circleSize)

Change circleSize directly in your buttons.

BTW: The terniary operator is Apple's recommended way of doing such changes.

2      

Hi! In Button you have to update @State private var circleSize: CGFloat = 20. And Circle will use var circleNewSize. Once you press the button, it will update circleSize, and circleNewSize being computed property will change. But this is redundant, you can use circleSize in both cases and the result will be the same.

2      

@Hatsushira

Thanks for the quick help. I know that the terniary operator solves this problem. But I only chose this example to illustrate my problem. I want to process a logic after using a view element with a monitored variable. As soon as this variable is changed, several actions are to be executed.

2      

@ygeras Thank you for quick help. I only chose this example to illustrate my problem. I want to process a logic after using a view element with a monitored variable. As soon as this variable is changed, several actions are to be executed.

2      

Maybe it is better to use this approach?

struct ContentView: View {
    @State private var circleSize: CGFloat = 20
    @State private var circleColor: Color = .blue

    var body: some View {
        VStack {
            Circle()
                .foregroundColor(circleColor)
                .frame(width: circleSize, height: circleSize)

            HStack {
                Button(action: {
                    circleSize += 1
                }) {
                    Text("+")
                        .font(.largeTitle)
                }

                Button(action: {
                    circleSize -= 1
                }) {
                    Text("-")
                        .font(.largeTitle)
                }
            }
        }
        .onChange(of: circleSize) { oldValue, newValue in
            // here you can run whatever logic you might need after circleSize has been updated
            if circleSize > 25 {
                circleColor = .red
            } else {
                circleColor = .blue
            }
        }
    }
}

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!

Reply to this topic…

You need to create an account or log in to reply.

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.