我有一个像
这样的结构struct LoginPage {
static let usernameField = Element("#username")
}
我有一个类,
class LoginView {
func enterCredentails () {
LoginPage.usernameField.waitForExist()
}
}
并且Element
api看起来像
class Element {
init(...) {
...
}
func waitForExist () {
// print("caller property name")
}
}
...在waitForExist()
里面我希望得到触发此方法的属性名称(usernameField
)。这样我就可以根据属性名称在error
内动态打印success
和waitForExist()
消息。
注意:我尝试使用Mirror
获取所有属性(以便我可以根据Element
存储和检索密钥名称),但它没有返回static
属性(Referred)。所以我正在寻找替代解决方案。
先谢谢你帮助我!
答案 0 :(得分:-1)
我建议采用简单的方法:
func waitForExist(propertyName: String [or possibly make it an enum]) {
// do what needs to be done
}
我假设你的属性在didSet
或其他东西中调用它;只需让每一个都将适当的值传递给waitForExist
函数。
至于你的问题的字面答案,你可能可能会使用像backtrace()
这样的函数检查堆栈,但是ehhhhhh ...相信我,你真的不想做这一点。