NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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??

1      

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.

1      

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!

Cheers

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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.