|
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.
|
|
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.
Change circleSize directly in your buttons. BTW: The terniary operator is Apple's recommended way of doing such changes. |
|
Hi! In Button you have to update |
|
@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. |
|
@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. |
|
Maybe it is better to use this approach?
|
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.