Steven Hernandez Steven Hernandez - 5 months ago 48
Swift Question

Swift: Get Variable Actual Name as String

So I am trying to get the Actual Variable Name as String in Swift, but have not found a way to do so... or maybe I am looking at this problem and solution in a bad angle.

So this is basically what I want to do:

var appId: String? = nil

//This is true, since appId is actually the name of the var appId
if( appId.getVarName = "appId"){
appId = "CommandoFurball"

Unfortunately I have not been able to find in apple docs anything that is close to this but this:

varobj.self or reflect(var).summary

however, this gives information of what is inside the variable itself or the type of the variable in this case being String and I want the Actual name of the Variable.

Appreciate the help :)

Answer Source

As per the updated from this answer, it is supported in Swift 3 via #keyPath

NSPredicate(format: "%K == %@", #keyPath(Person.firstName), "Andrew")