有没有办法在 Swift 4 中从字符串创建 Keypath,以通过路径或变量名访问结构中的值
Finally I found out that I should use CodingKeys instead of KeyPaths to access the a value of a variable of a struct by 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)
关于swift4 - 来自字符串的 Swift Keypath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52427304/