5

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

最后我发现我应该使用 CodingKeys 而不是 KeyPaths 来通过 String 访问结构的变量的值

在此先感谢,迈克尔

4

1 回答 1

0

考虑你有这样的东西,

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)
于 2018-09-20T14:49:36.227 回答