简体   繁体   English

swift 中的 \ 是什么意思?

[英]What does \ mean in swift?

I'm new to swift.我是 swift 的新手。

I'm pretty sure I understand what \ does in code like:我很确定我理解 \ 在代码中的作用,例如:

@Environment(\.managedObjectContext)
ForEach(example, id: \.id) {
//etc.
}

But what does \ actually mean?但是 \ 实际上是什么意思? Is there some way to memorise it in your head in a meaningful way?有什么方法可以有意义地记住它吗?

It is swift KeyPath是 swift KeyPath

struct Guitar {
    let model: String
    let year: Int
}
let gibson = Guitar(model: "Gibson", year: 1990)
let path = \Guitar.model /// KeyPath<Guitar, String>

print(gibson[keyPath: path])
// prints “Gibson”

Example take and you can read more details in https://medium.com/@jllnmercier/swift-keypaths-db326852d66a以示例为例,您可以在https://medium.com/@jllnmercier/swift-keypaths-db326852d66a中阅读更多详细信息

and search keywords "swift keypath"并搜索关键字“swift keypath”

For your snapshot为您的快照

ForEach(example, id: \.id) {
//etc.
}

it means that ForEach will use example.Element.id as identifier for each iterated row.这意味着ForEach将使用example.Element.id作为每个迭代行的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM