Confused Confused - 30 days ago 16
Swift Question

Convert Swift variable name to string, to use as string?

I've got a texture to import that's named the same as the variable boolean that represents its state.

let myButtonNameABC = false

is there a way to convert the name of this variable to a String so it can be used to load the images that are named the same?

Answer Source

You can make this a function instead of a variable, and do the same thing. I'll explain it in steps.

So first, how do we make a function the same thing as a variable?

Basically, we are "wrapping" the variable inside of the function. This is a similar to approach to the first block I suggested in your latest RefBool thread, where we grabbed or set a value via function.

Here, we are using methods/functions to alter a variable directly without parameters--so, we will be using the function by name instead of using the variable by name to access it throughout our code:

enum Static {
  private static var _button1 = false
  static func button1() { toggle(&_button1) }

Note, the Enum is just a namespace for us to call the method to emphasize that the variable's name is not needed for our code to work--Only the function needs to know its name.This code would work inside of a struct or class as well, or even in a global namespace.

So here, if I want to change the value of _button1, I must call button1(). _button1 is essentially invisible to everyone and everything--and that's ok, because we only need button1() to do our work.

So now, every time we call Static.button1() , Static._button1 will toggle true to false, false to true, and so on (using the function I gave you earlier)

Why are we doing this?

Well, because there is no way to get the name of a variable without Mirror and relction, and there is already a built in command called #function. We are jumping through the added hoop of using the function, so we don't have to set-up a mirror and other OBJc hoops.

Let's alter our enum to demonstrate how we can get the name of the func:

 enum Static {
  private static var _button1 = false

  static func button1() -> String { 
    toggle ( &_button1 ) 
    return ( #function )

Now, when we call button1(), we do two things: 1, we toggle the state of the button (_button1), and we return a string with the name "button1()"

let buttonName = Static.button1()  // buttonName = "button1()"

We can make this more usable by calling a member of String to mutate itself:

let trimmedButtonName = Static.button1().replacingOccurrences(of: "()", with: "")
// trimmedButtonName = "button1"

Let's update our enum now with this handy replacement:

enum Static {
  private static var _button1 = false

  static func button1() -> String { 
    toggle ( &_button1 ) 
    return ( #function.replacingOccurrences(of: "()", with: "") )

And our final use case is as follows:

let buttonName = Static.button1()


you don't have to have _button1 as private, that was just to demonstrate that you don't need it to be public. If you need to grab the state of it, you can make another function, or return a tuple type from button1():

static func button1() -> (name: String, state: Bool) {
// ...        
  return ( #function.etc, _button1)

let results = Static.button1()
print(  ) // "button1"
print( results.state ) //  true / false

You can also set something in the parameter if you need more explicit control over setting the variable, just as you would any normal function.



For example, if you wanted explicit control, you could do:

func button1(equals: Bool? = nil) -> (name: String, state: Bool) {
  if equals != nil {
    // do the code mentioned above
  } else {
    _button1 = equals!

  return (#function.replacingOccurrences(of: "()", with: ""), _button1)

let buttonState.state = Static.button1(equals: false) // false
let buttonStateAgain.state = Static.button1(equals: true) // true
print(Static.button1().name) // "button1"