Swift 来自字符串的键路径

Swift Keypath from String

有没有办法从 Swift 中的字符串创建键路径 4 以通过其路径或变量名访问结构中的值

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)