gpt4 book ai didi

swift4 - 来自字符串的 Swift Keypath

转载 作者:行者123 更新时间:2023-12-02 09:03:56 26 4
gpt4 key购买 nike

有没有办法在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com