Joseph Beuys' Mum
Joseph Beuys' Mum

Reputation: 2264

Is it possible to evaluate a var/let with Swift?

Based upon this question: << Is the a ScriptEngine or eval()-like function in Swift? >> I'm assuming the following would not work in Swift:

private let BROWN_COLOUR: UIColor = UIColor.brownColor()

...

var colourName: String = "BROWN"
var colour = self[ colourName + "_COLOUR" ] as UIColor!

Correct or not?

Thank you for reading.

Upvotes: 0

Views: 647

Answers (1)

DarkDust
DarkDust

Reputation: 92384

No, you cannot do that out of the box. You can, however, use keyed subscription to solve this. Or even easier: use a dictionary to store your values and query that.

But if the object derives from NSObject and the instance variables are marked with @objc, you can query these instance variables with valueForKey.

Upvotes: 2

Related Questions