BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

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!



Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn 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.