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

Refer To A Property Dynamically

Forums > SwiftUI

Hi, Struggling to explain what I need to do do but here goes.

If I have three properties eg...

var user01 = false
var user02 = false
var user03 = false

A function in my app returns one of three values - 01, 02 or 03 - as theValue I then want to use theValue and cocatenate with "user" to update one of the properties.

eg user\(theValue) = true

I know that the above line of script is nonsense. Any pointers on how this behaviour can be achieved??


This is not possible.

Maybe if you describe in more detail what you need to achieve, we can suggest you better solution?

You could also look into using a Dictionary where the keys would be "user01", "user02" etc and then you could dynamically construct these keys and update the properties.


Well as @nemecek-filip mentions it is not possible to do that.

Dictionary is a good way to go about it, although this will add a layer of dealing with optionals.

It would help greatly if you share what the function does. What is its purpose?

Then we could recommend the best approach!



TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.