BLACK FRIDAY: Save 50% 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??

3      

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.

3      

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

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.