小编典典

获取Swift变量的实际名称作为字符串

swift

因此,我试图在Swift中将“实际变量名称”获取为String,但是还没有找到一种方法…或者也许我正以一个不好的角度看待这个问题和解决方案。

所以这基本上是我想要做的:

var appId: String? = nil

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

不幸的是,我无法在苹果文档中找到任何与之接近的东西,但这是:

varobj.self or reflect(var).summary

但是,这给出了变量本身或变量类型的信息,在这种情况下为字符串,我想要变量的实际名称。


阅读 531

收藏
2020-07-07

共1个答案

小编典典

根据更新,它在Swift 3中通过#keyPath

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