有没有办法在 Swift 4 中从字符串创建 Keypath,以通过路径或变量名访问结构中的值
最后我发现我应该使用 CodingKeys 而不是 KeyPaths
通过 String 访问结构体变量的值
提前致谢,
迈克尔
考虑你有这样的东西,
struct foo {
var test: doo
}
struct doo {
var test: Int
}
//How to use it
let doo = Doo(test: 10)
let foo = Foo(test: doo)
let mykeyPath = \Foo.test.test
let result = foo[keyPath: mykeyPath]
print(result)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)